<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="python绘图及可视化备课, 十二惊惶的gitee">
    <meta name="description" content="python绘图及数据可视化备课[TOC]
补充 上次实验课的库与爬虫于刊老师担心我准备的内容不够讲两节课的，如果我讲完这一章的内容还没有结束的话我就讲一讲我最近捣鼓的东西
1 jieba库进行词频分析一、jieba库基本介绍
 jieba">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="referrer" content="no-referrer-when-downgrade">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>python绘图及可视化备课 | 十二惊惶的gitee</title>
    <link rel="icon" type="image/png" href="/shier_jinghuang/favicon.png">
    


    <!-- bg-cover style     -->



<link rel="stylesheet" type="text/css" href="/shier_jinghuang/libs/awesome/css/all.min.css">
<link rel="stylesheet" type="text/css" href="/shier_jinghuang/libs/materialize/materialize.min.css">
<link rel="stylesheet" type="text/css" href="/shier_jinghuang/libs/aos/aos.css">
<link rel="stylesheet" type="text/css" href="/shier_jinghuang/libs/animate/animate.min.css">
<link rel="stylesheet" type="text/css" href="/shier_jinghuang/libs/lightGallery/css/lightgallery.min.css">
<link rel="stylesheet" type="text/css" href="/shier_jinghuang/css/matery.css">
<link rel="stylesheet" type="text/css" href="/shier_jinghuang/css/my.css">
<link rel="stylesheet" type="text/css" href="/shier_jinghuang/css/dark.css" media="none" onload="if(media!='all')media='all'">




    <link rel="stylesheet" href="/shier_jinghuang/libs/tocbot/tocbot.css">
    <link rel="stylesheet" href="/shier_jinghuang/css/post.css">




    



    <script src="/shier_jinghuang/libs/jquery/jquery-3.6.0.min.js"></script>

<meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/shier_jinghuang/atom.xml" title="十二惊惶的gitee" type="application/atom+xml">
</head>


<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/shier_jinghuang/" class="waves-effect waves-light">
                    
                    <img src="/shier_jinghuang/medias/1.jpg" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">十二惊惶的gitee</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shier_jinghuang/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shier_jinghuang/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shier_jinghuang/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shier_jinghuang/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shier_jinghuang/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shier_jinghuang/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shier_jinghuang/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
  <li>
    <a href="javascript:;" class="waves-effect waves-light" onclick="switchNightMode()" title="深色/浅色模式" >
      <i id="sum-moon-icon" class="fas fa-sun" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/shier_jinghuang/medias/1.jpg" class="logo-img circle responsive-img">
        
        <div class="logo-name">十二惊惶的gitee</div>
        <div class="logo-desc">
            
            犁牛之子骍且角，虽欲勿用，山川其舍诸
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/shier_jinghuang/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/shier_jinghuang/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/shier_jinghuang/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/shier_jinghuang/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/shier_jinghuang/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/shier_jinghuang/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			留言板
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/shier_jinghuang/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://gitee.com/Im-so-scared-2/shier_jinghuang" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>十二惊惶
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://gitee.com/Im-so-scared-2/shier_jinghuang" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="十二惊惶" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/shier_jinghuang/medias/featureimages/6.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">python绘图及可视化备课</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/shier_jinghuang/tags/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/">
                                <span class="chip bg-color">基础知识</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/shier_jinghuang/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" class="post-category">
                                学习笔记
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2021-11-06
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2021-11-13
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    10.6k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    47 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/shier_jinghuang/libs/prism/prism.min.css">
        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h1 id="python绘图及数据可视化备课"><a href="#python绘图及数据可视化备课" class="headerlink" title="python绘图及数据可视化备课"></a>python绘图及数据可视化备课</h1><p>[TOC]</p>
<h2 id="补充-上次实验课的库与爬虫"><a href="#补充-上次实验课的库与爬虫" class="headerlink" title="补充 上次实验课的库与爬虫"></a>补充 上次实验课的库与爬虫</h2><p>于刊老师担心我准备的内容不够讲两节课的，如果我讲完这一章的内容还没有结束的话我就讲一讲我最近捣鼓的东西</p>
<h3 id="1-jieba库进行词频分析"><a href="#1-jieba库进行词频分析" class="headerlink" title="1 jieba库进行词频分析"></a>1 jieba库进行词频分析</h3><h4 id="一、jieba库基本介绍"><a href="#一、jieba库基本介绍" class="headerlink" title="一、jieba库基本介绍"></a>一、jieba库基本介绍</h4><blockquote>
<p> jieba是优秀的中文分词第三方库，需要额外安装，安装方式为<strong>pip install jieba</strong>,jieba库提供三种分词模式，最简单只需掌握一个函数</p>
<p>利用一个中文词库，可以确定汉字之间的关联概率,汉字间概率大的组成词组，形成分词结果</p>
</blockquote>
<h4 id="二、jieba库使用说明"><a href="#二、jieba库使用说明" class="headerlink" title="二、jieba库使用说明"></a>二、jieba库使用说明</h4><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token comment">#精确模式、全模式、搜索引擎模式精确模式：</span>
<span class="token comment">#把文本精确的切分开，不存在冗余单词</span>
<span class="token comment">#全模式：把文本中所有可能的词语都扫描出来，有冗余</span>
<span class="token comment">#搜索引擎模式：在精确模式基础上，对长词再次切分</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>把文本精确的切分开，不存在冗余单词</p>
<p>全模式：把文本中所有可能的词语都扫描出来，有冗余</p>
<p>搜索引擎模式：在精确模式基础上，对长词再次切分</p>
<p><strong>jieba库常用函数</strong> <img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/1.png"> </p>
<h3 id="2-re库进行正则匹配"><a href="#2-re库进行正则匹配" class="headerlink" title="2 re库进行正则匹配"></a><strong>2 re库进行正则匹配</strong></h3><p><strong>正则表达式（英文名称：regular expression，regex，RE）是用来简洁表达一组字符串特征的表达式。最主要应用在字符串匹配中。</strong></p>
<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/2.png"> </p>
<img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/3.png"  /> 

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/4.png"> </p>
<p>在这里需要注意，match对象是一种特殊的类型，如**re.search()<strong>与</strong>re.match()<strong>虽然也能做到类似的效果，但实际使用是有问题的，在这里使用的</strong>findall()**函数实现。</p>
<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/5.png"> </p>
<img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/6.png"  /> 

<h5 id="校验数字的表达式"><a href="#校验数字的表达式" class="headerlink" title="校验数字的表达式"></a>校验数字的表达式</h5><pre class="line-numbers language-python" data-language="python"><code class="language-python">数字：<span class="token operator">^</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\<span class="token operator">*</span>$
n位的数字：<span class="token operator">^</span>\d<span class="token punctuation">&#123;</span>n<span class="token punctuation">&#125;</span>$
至少n位的数字：<span class="token operator">^</span>\d<span class="token punctuation">&#123;</span>n<span class="token punctuation">,</span><span class="token punctuation">&#125;</span>$
m<span class="token operator">-</span>n位的数字：<span class="token operator">^</span>\d<span class="token punctuation">&#123;</span>m<span class="token punctuation">,</span>n<span class="token punctuation">&#125;</span>$
零和非零开头的数字：<span class="token operator">^</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token operator">|</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\<span class="token operator">*</span><span class="token punctuation">)</span>$
非零开头的最多带两位小数的数字：<span class="token operator">^</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\<span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token punctuation">(</span>\<span class="token punctuation">.</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span>?$
带<span class="token number">1</span><span class="token operator">-</span><span class="token number">2</span>位小数的正数或负数：<span class="token operator">^</span><span class="token punctuation">(</span>\<span class="token operator">-</span><span class="token punctuation">)</span>?\d<span class="token operator">+</span><span class="token punctuation">(</span>\<span class="token punctuation">.</span>\d<span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span>$
正数、负数、和小数：<span class="token operator">^</span><span class="token punctuation">(</span>\<span class="token operator">-</span><span class="token operator">|</span>\<span class="token operator">+</span><span class="token punctuation">)</span>?\d<span class="token operator">+</span><span class="token punctuation">(</span>\<span class="token punctuation">.</span>\d<span class="token operator">+</span><span class="token punctuation">)</span>?$
有两位小数的正实数：<span class="token operator">^</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token punctuation">(</span>\<span class="token punctuation">.</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">&#123;</span><span class="token number">2</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span>?$
有<span class="token number">1</span><span class="token operator">~</span><span class="token number">3</span>位小数的正实数：<span class="token operator">^</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token punctuation">(</span>\<span class="token punctuation">.</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span>?$
非零的正整数：<span class="token operator">^</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\d\<span class="token operator">*</span>$ 或 <span class="token operator">^</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\<span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">&#125;</span>$ 或 <span class="token operator">^</span>\<span class="token operator">+</span>?<span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\<span class="token operator">*</span>$
非零的负整数：<span class="token operator">^</span>\<span class="token operator">-</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span>"\<span class="token operator">*</span>$ 或 <span class="token operator">^</span><span class="token operator">-</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\d\<span class="token operator">*</span>$
非负整数：<span class="token operator">^</span>\d<span class="token operator">+</span>$ 或 <span class="token operator">^</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\d\<span class="token operator">*</span><span class="token operator">|</span><span class="token number">0</span>$
非正整数：<span class="token operator">^</span><span class="token operator">-</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\d\<span class="token operator">*</span><span class="token operator">|</span><span class="token number">0</span>$ 或 <span class="token operator">^</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token operator">-</span>\d<span class="token operator">+</span><span class="token punctuation">)</span><span class="token operator">|</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token operator">+</span><span class="token punctuation">)</span><span class="token punctuation">)</span>$
非负浮点数：<span class="token operator">^</span>\d<span class="token operator">+</span><span class="token punctuation">(</span>\<span class="token punctuation">.</span>\d<span class="token operator">+</span><span class="token punctuation">)</span>?$ 或 <span class="token operator">^</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\d\<span class="token operator">*</span>\<span class="token punctuation">.</span>\d\<span class="token operator">*</span><span class="token operator">|</span><span class="token number">0</span>\<span class="token punctuation">.</span>\d\<span class="token operator">*</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\d\<span class="token operator">*</span><span class="token operator">|</span><span class="token number">0</span>?\<span class="token number">.0</span><span class="token operator">+</span><span class="token operator">|</span><span class="token number">0</span>$
非正浮点数：<span class="token operator">^</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token operator">-</span>\d<span class="token operator">+</span><span class="token punctuation">(</span>\<span class="token punctuation">.</span>\d<span class="token operator">+</span><span class="token punctuation">)</span>?<span class="token punctuation">)</span><span class="token operator">|</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token operator">+</span><span class="token punctuation">(</span>\<span class="token number">.0</span><span class="token operator">+</span><span class="token punctuation">)</span>?<span class="token punctuation">)</span><span class="token punctuation">)</span>$ 或 <span class="token operator">^</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\d\<span class="token operator">*</span>\<span class="token punctuation">.</span>\d\<span class="token operator">*</span><span class="token operator">|</span><span class="token number">0</span>\<span class="token punctuation">.</span>\d\<span class="token operator">*</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\d\<span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">|</span><span class="token number">0</span>?\<span class="token number">.0</span><span class="token operator">+</span><span class="token operator">|</span><span class="token number">0</span>$
正浮点数：<span class="token operator">^</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\d\<span class="token operator">*</span>\<span class="token punctuation">.</span>\d\<span class="token operator">*</span><span class="token operator">|</span><span class="token number">0</span>\<span class="token punctuation">.</span>\d\<span class="token operator">*</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\d\<span class="token operator">*</span>$ 或 <span class="token operator">^</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token operator">+</span>\<span class="token punctuation">.</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\<span class="token operator">*</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\<span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">|</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\<span class="token operator">*</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\<span class="token operator">*</span>\<span class="token punctuation">.</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token punctuation">)</span><span class="token operator">|</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\<span class="token operator">*</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\<span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">)</span>$
<span class="token operator">-</span> 负浮点数：<span class="token operator">^</span><span class="token operator">-</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\d\<span class="token operator">*</span>\<span class="token punctuation">.</span>\d\<span class="token operator">*</span><span class="token operator">|</span><span class="token number">0</span>\<span class="token punctuation">.</span>\d\<span class="token operator">*</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\d\<span class="token operator">*</span><span class="token punctuation">)</span>$ 或 <span class="token operator">^</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token operator">+</span>\<span class="token punctuation">.</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\<span class="token operator">*</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\<span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">|</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\<span class="token operator">*</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\<span class="token operator">*</span>\<span class="token punctuation">.</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token punctuation">)</span><span class="token operator">|</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\<span class="token operator">*</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\<span class="token operator">*</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>$
<span class="token operator">-</span> 浮点数：<span class="token operator">^</span><span class="token punctuation">(</span><span class="token operator">-</span>?\d<span class="token operator">+</span><span class="token punctuation">)</span><span class="token punctuation">(</span>\<span class="token punctuation">.</span>\d<span class="token operator">+</span><span class="token punctuation">)</span>?$ 或 <span class="token operator">^</span><span class="token operator">-</span>?<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\d\<span class="token operator">*</span>\<span class="token punctuation">.</span>\d\<span class="token operator">*</span><span class="token operator">|</span><span class="token number">0</span>\<span class="token punctuation">.</span>\d\<span class="token operator">*</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span>\d\<span class="token operator">*</span><span class="token operator">|</span><span class="token number">0</span>?\<span class="token number">.0</span><span class="token operator">+</span><span class="token operator">|</span><span class="token number">0</span><span class="token punctuation">)</span>$<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<hr>
<h5 id="校验字符的表达式"><a href="#校验字符的表达式" class="headerlink" title="校验字符的表达式"></a>校验字符的表达式</h5><pre class="line-numbers language-python" data-language="python"><code class="language-python">汉字：<span class="token operator">^</span><span class="token punctuation">[</span>\u4e00<span class="token operator">-</span>\u9fa5<span class="token punctuation">]</span><span class="token punctuation">&#123;</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token punctuation">&#125;</span>$
英文和数字：<span class="token operator">^</span><span class="token punctuation">[</span>A<span class="token operator">-</span>Za<span class="token operator">-</span>z0<span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token operator">+</span>$ 或 <span class="token operator">^</span><span class="token punctuation">[</span>A<span class="token operator">-</span>Za<span class="token operator">-</span>z0<span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">&#123;</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">40</span><span class="token punctuation">&#125;</span>$
长度为<span class="token number">3</span><span class="token operator">-</span><span class="token number">20</span>的所有字符：<span class="token operator">^</span><span class="token punctuation">.</span><span class="token punctuation">&#123;</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">&#125;</span>$
由<span class="token number">26</span>个英文字母组成的字符串：<span class="token operator">^</span><span class="token punctuation">[</span>A<span class="token operator">-</span>Za<span class="token operator">-</span>z<span class="token punctuation">]</span><span class="token operator">+</span>$
由<span class="token number">26</span>个大写英文字母组成的字符串：<span class="token operator">^</span><span class="token punctuation">[</span>A<span class="token operator">-</span>Z<span class="token punctuation">]</span><span class="token operator">+</span>$
由<span class="token number">26</span>个小写英文字母组成的字符串：<span class="token operator">^</span><span class="token punctuation">[</span>a<span class="token operator">-</span>z<span class="token punctuation">]</span><span class="token operator">+</span>$
由数字和<span class="token number">26</span>个英文字母组成的字符串：<span class="token operator">^</span><span class="token punctuation">[</span>A<span class="token operator">-</span>Za<span class="token operator">-</span>z0<span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token operator">+</span>$
由数字、<span class="token number">26</span>个英文字母或者下划线组成的字符串：<span class="token operator">^</span>\w<span class="token operator">+</span>$ 或 <span class="token operator">^</span>\w<span class="token punctuation">&#123;</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">&#125;</span>$
中文、英文、数字包括下划线：<span class="token operator">^</span><span class="token punctuation">[</span>\u4E00<span class="token operator">-</span>\u9FA5A<span class="token operator">-</span>Za<span class="token operator">-</span>z0<span class="token operator">-</span>9_<span class="token punctuation">]</span><span class="token operator">+</span>$
中文、英文、数字但不包括下划线等符号：<span class="token operator">^</span><span class="token punctuation">[</span>\u4E00<span class="token operator">-</span>\u9FA5A<span class="token operator">-</span>Za<span class="token operator">-</span>z0<span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token operator">+</span>$ 或 <span class="token operator">^</span><span class="token punctuation">[</span>\u4E00<span class="token operator">-</span>\u9FA5A<span class="token operator">-</span>Za<span class="token operator">-</span>z0<span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">&#123;</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">&#125;</span>$
可以输入含有<span class="token operator">^</span><span class="token operator">%</span><span class="token operator">&amp;</span><span class="token string">',;=?$\"等字符：**[^%&amp;'</span><span class="token punctuation">,</span><span class="token punctuation">;</span><span class="token operator">=</span>?$\x22<span class="token punctuation">]</span><span class="token operator">+</span>
禁止输入含有<span class="token operator">~</span>的字符：<span class="token punctuation">[</span><span class="token operator">^</span><span class="token operator">~</span>\x22<span class="token punctuation">]</span><span class="token operator">+</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<hr>
<h5 id="特殊需求表达式"><a href="#特殊需求表达式" class="headerlink" title="特殊需求表达式"></a>特殊需求表达式</h5><pre class="line-numbers language-python" data-language="python"><code class="language-python">Email地址：<span class="token operator">**</span><span class="token operator">^</span>\w<span class="token operator">+</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token operator">+</span><span class="token punctuation">.</span><span class="token punctuation">]</span>\w<span class="token operator">+</span><span class="token punctuation">)</span>\<span class="token operator">*</span>@\w<span class="token operator">+</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token punctuation">.</span><span class="token punctuation">]</span>\w<span class="token operator">+</span><span class="token punctuation">)</span>\<span class="token operator">*</span>\<span class="token punctuation">.</span>\w<span class="token operator">+</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token punctuation">.</span><span class="token punctuation">]</span>\w<span class="token operator">+</span><span class="token punctuation">)</span>\<span class="token operator">*</span>$<span class="token operator">**</span>
域名：<span class="token operator">**</span><span class="token punctuation">[</span>a<span class="token operator">-</span>zA<span class="token operator">-</span>Z0<span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span>a<span class="token operator">-</span>zA<span class="token operator">-</span>Z0<span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">&#123;</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">62</span><span class="token punctuation">&#125;</span><span class="token punctuation">(</span>\<span class="token punctuation">.</span><span class="token punctuation">[</span>a<span class="token operator">-</span>zA<span class="token operator">-</span>Z0<span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span>a<span class="token operator">-</span>zA<span class="token operator">-</span>Z0<span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">&#123;</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">62</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token operator">+</span>\<span class="token punctuation">.</span>?<span class="token operator">**</span>
InternetURL：<span class="token operator">**</span><span class="token punctuation">[</span>a<span class="token operator">-</span>zA<span class="token operator">-</span>z<span class="token punctuation">]</span><span class="token operator">+</span><span class="token punctuation">:</span><span class="token operator">//</span><span class="token punctuation">[</span><span class="token operator">^</span>\s<span class="token punctuation">]</span>\<span class="token operator">*</span> 或 <span class="token operator">^</span>http<span class="token punctuation">:</span><span class="token operator">//</span><span class="token punctuation">(</span><span class="token punctuation">[</span>\w<span class="token operator">-</span><span class="token punctuation">]</span><span class="token operator">+</span>\<span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token punctuation">[</span>\w<span class="token operator">-</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token punctuation">(</span><span class="token operator">/</span><span class="token punctuation">[</span>\w<span class="token operator">-</span><span class="token punctuation">.</span><span class="token operator">/</span>?<span class="token operator">%</span><span class="token operator">&amp;</span><span class="token operator">=</span><span class="token punctuation">]</span>\<span class="token operator">*</span><span class="token punctuation">)</span>?$<span class="token operator">**</span>
手机号码：<span class="token operator">**</span><span class="token operator">^</span><span class="token punctuation">(</span><span class="token number">13</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token operator">|</span><span class="token number">14</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token operator">|</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token operator">|</span><span class="token number">15</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">|</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">2</span><span class="token operator">|</span><span class="token number">3</span><span class="token operator">|</span><span class="token number">4</span><span class="token operator">|</span><span class="token number">5</span><span class="token operator">|</span><span class="token number">6</span><span class="token operator">|</span><span class="token number">7</span><span class="token operator">|</span><span class="token number">8</span><span class="token operator">|</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token operator">|</span><span class="token number">18</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">|</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">2</span><span class="token operator">|</span><span class="token number">3</span><span class="token operator">|</span><span class="token number">5</span><span class="token operator">|</span><span class="token number">6</span><span class="token operator">|</span><span class="token number">7</span><span class="token operator">|</span><span class="token number">8</span><span class="token operator">|</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">)</span>\d<span class="token punctuation">&#123;</span><span class="token number">8</span><span class="token punctuation">&#125;</span>$<span class="token operator">**</span>
电话号码<span class="token punctuation">(</span><span class="token string">"XXX-XXXXXXX"</span>、<span class="token string">"XXXX-XXXXXXXX"</span>、<span class="token string">"XXX-XXXXXXX"</span>、<span class="token string">"XXX-XXXXXXXX"</span>、<span class="token string">"XXXXXXX"</span>和"XXXXXXXX<span class="token punctuation">)</span>：<span class="token operator">**</span><span class="token operator">^</span><span class="token punctuation">(</span>\<span class="token punctuation">(</span>\d<span class="token punctuation">&#123;</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">&#125;</span><span class="token operator">-</span><span class="token punctuation">)</span><span class="token operator">|</span>\d<span class="token punctuation">&#123;</span><span class="token number">3.4</span><span class="token punctuation">&#125;</span><span class="token operator">-</span><span class="token punctuation">)</span>?\d<span class="token punctuation">&#123;</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">&#125;</span>$<span class="token operator">**</span>
国内电话号码<span class="token punctuation">(</span><span class="token number">0511</span><span class="token operator">-</span><span class="token number">4405222</span>、<span class="token number">021</span><span class="token operator">-</span><span class="token number">87888822</span><span class="token punctuation">)</span>：<span class="token operator">**</span>\d<span class="token punctuation">&#123;</span><span class="token number">3</span><span class="token punctuation">&#125;</span><span class="token operator">-</span>\d<span class="token punctuation">&#123;</span><span class="token number">8</span><span class="token punctuation">&#125;</span><span class="token operator">|</span>\d<span class="token punctuation">&#123;</span><span class="token number">4</span><span class="token punctuation">&#125;</span><span class="token operator">-</span>\d<span class="token punctuation">&#123;</span><span class="token number">7</span><span class="token punctuation">&#125;</span><span class="token operator">**</span>
电话号码正则表达式（支持手机号码，<span class="token number">3</span><span class="token operator">-</span><span class="token number">4</span>位区号，<span class="token number">7</span><span class="token operator">-</span><span class="token number">8</span>位直播号码，<span class="token number">1</span>－<span class="token number">4</span>位分机号）<span class="token punctuation">:</span> <span class="token operator">**</span><span class="token punctuation">(</span><span class="token punctuation">(</span>\d<span class="token punctuation">&#123;</span><span class="token number">11</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token operator">|</span><span class="token operator">^</span><span class="token punctuation">(</span><span class="token punctuation">(</span>\d<span class="token punctuation">&#123;</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token operator">|</span><span class="token punctuation">(</span>\d<span class="token punctuation">&#123;</span><span class="token number">4</span><span class="token punctuation">&#125;</span><span class="token operator">|</span>\d<span class="token punctuation">&#123;</span><span class="token number">3</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token punctuation">(</span>\d<span class="token punctuation">&#123;</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token operator">|</span><span class="token punctuation">(</span>\d<span class="token punctuation">&#123;</span><span class="token number">4</span><span class="token punctuation">&#125;</span><span class="token operator">|</span>\d<span class="token punctuation">&#123;</span><span class="token number">3</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token punctuation">(</span>\d<span class="token punctuation">&#123;</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token punctuation">(</span>\d<span class="token punctuation">&#123;</span><span class="token number">4</span><span class="token punctuation">&#125;</span><span class="token operator">|</span>\d<span class="token punctuation">&#123;</span><span class="token number">3</span><span class="token punctuation">&#125;</span><span class="token operator">|</span>\d<span class="token punctuation">&#123;</span><span class="token number">2</span><span class="token punctuation">&#125;</span><span class="token operator">|</span>\d<span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token operator">|</span><span class="token punctuation">(</span>\d<span class="token punctuation">&#123;</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token punctuation">(</span>\d<span class="token punctuation">&#123;</span><span class="token number">4</span><span class="token punctuation">&#125;</span><span class="token operator">|</span>\d<span class="token punctuation">&#123;</span><span class="token number">3</span><span class="token punctuation">&#125;</span><span class="token operator">|</span>\d<span class="token punctuation">&#123;</span><span class="token number">2</span><span class="token punctuation">&#125;</span><span class="token operator">|</span>\d<span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>$<span class="token punctuation">)</span><span class="token operator">**</span>
身份证号<span class="token punctuation">(</span><span class="token number">15</span>位、<span class="token number">18</span>位数字<span class="token punctuation">)</span>，最后一位是校验位，可能为数字或字符X：<span class="token operator">**</span><span class="token punctuation">(</span><span class="token operator">^</span>\d<span class="token punctuation">&#123;</span><span class="token number">15</span><span class="token punctuation">&#125;</span>$<span class="token punctuation">)</span><span class="token operator">|</span><span class="token punctuation">(</span><span class="token operator">^</span>\d<span class="token punctuation">&#123;</span><span class="token number">18</span><span class="token punctuation">&#125;</span>$<span class="token punctuation">)</span><span class="token operator">|</span><span class="token punctuation">(</span><span class="token operator">^</span>\d<span class="token punctuation">&#123;</span><span class="token number">17</span><span class="token punctuation">&#125;</span><span class="token punctuation">(</span>\d<span class="token operator">|</span>X<span class="token operator">|</span>x<span class="token punctuation">)</span>$<span class="token punctuation">)</span><span class="token operator">**</span>
帐号是否合法<span class="token punctuation">(</span>字母开头，允许<span class="token number">5</span><span class="token operator">-</span><span class="token number">16</span>字节，允许字母数字下划线<span class="token punctuation">)</span>：<span class="token operator">**</span><span class="token operator">^</span><span class="token punctuation">[</span>a<span class="token operator">-</span>zA<span class="token operator">-</span>Z<span class="token punctuation">]</span><span class="token punctuation">[</span>a<span class="token operator">-</span>zA<span class="token operator">-</span>Z0<span class="token operator">-</span>9_<span class="token punctuation">]</span><span class="token punctuation">&#123;</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">15</span><span class="token punctuation">&#125;</span>$<span class="token operator">**</span>
密码<span class="token punctuation">(</span>以字母开头，长度在<span class="token number">6</span><span class="token operator">~</span><span class="token number">18</span>之间，只能包含字母、数字和下划线<span class="token punctuation">)</span>：<span class="token operator">**</span><span class="token operator">^</span><span class="token punctuation">[</span>a<span class="token operator">-</span>zA<span class="token operator">-</span>Z<span class="token punctuation">]</span>\w<span class="token punctuation">&#123;</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">17</span><span class="token punctuation">&#125;</span>$<span class="token operator">**</span>
强密码<span class="token punctuation">(</span>必须包含大小写字母和数字的组合，不能使用特殊字符，长度在 <span class="token number">8</span><span class="token operator">-</span><span class="token number">10</span> 之间<span class="token punctuation">)</span>：<span class="token operator">**</span><span class="token operator">^</span><span class="token punctuation">(</span>?<span class="token operator">=</span><span class="token punctuation">.</span>\<span class="token operator">*</span>\d<span class="token punctuation">)</span><span class="token punctuation">(</span>?<span class="token operator">=</span><span class="token punctuation">.</span>\<span class="token operator">*</span><span class="token punctuation">[</span>a<span class="token operator">-</span>z<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">(</span>?<span class="token operator">=</span><span class="token punctuation">.</span>\<span class="token operator">*</span><span class="token punctuation">[</span>A<span class="token operator">-</span>Z<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">[</span>a<span class="token operator">-</span>zA<span class="token operator">-</span>Z0<span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">&#123;</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">&#125;</span>$<span class="token operator">**</span>
强密码<span class="token punctuation">(</span>必须包含大小写字母和数字的组合，可以使用特殊字符，长度在<span class="token number">8</span><span class="token operator">-</span><span class="token number">10</span>之间<span class="token punctuation">)</span>：<span class="token operator">**</span><span class="token operator">^</span><span class="token punctuation">(</span>?<span class="token operator">=</span><span class="token punctuation">.</span>\<span class="token operator">*</span>\d<span class="token punctuation">)</span><span class="token punctuation">(</span>?<span class="token operator">=</span><span class="token punctuation">.</span>\<span class="token operator">*</span><span class="token punctuation">[</span>a<span class="token operator">-</span>z<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">(</span>?<span class="token operator">=</span><span class="token punctuation">.</span>\<span class="token operator">*</span><span class="token punctuation">[</span>A<span class="token operator">-</span>Z<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token punctuation">&#123;</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">&#125;</span>$<span class="token operator">**</span>
日期格式：<span class="token operator">**</span><span class="token operator">^</span>\d<span class="token punctuation">&#123;</span><span class="token number">4</span><span class="token punctuation">&#125;</span><span class="token operator">-</span>\d<span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">&#125;</span><span class="token operator">-</span>\d<span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">&#125;</span><span class="token operator">**</span>
一年的<span class="token number">12</span>个月<span class="token punctuation">(</span><span class="token number">01</span>～<span class="token number">09</span>和<span class="token number">1</span>～<span class="token number">12</span><span class="token punctuation">)</span>：<span class="token operator">**</span><span class="token operator">^</span><span class="token punctuation">(</span><span class="token number">0</span>?<span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token operator">|</span><span class="token number">1</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span>$<span class="token operator">**</span>
一个月的<span class="token number">31</span>天<span class="token punctuation">(</span><span class="token number">01</span>～<span class="token number">09</span>和<span class="token number">1</span>～<span class="token number">31</span><span class="token punctuation">)</span>：<span class="token operator">**</span><span class="token operator">^</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">0</span>?<span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">|</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token operator">-</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">|</span><span class="token number">30</span><span class="token operator">|</span><span class="token number">31</span><span class="token punctuation">)</span>$<span class="token operator">**</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>在这里我们需要注意的是<strong>^[\u4e00-\u9fa5]{0,}$<strong>，这个是汉字的编码范围，在爬虫过程中经常会有一个爬目录的操作^http://([\w-]+.)+[\w-]+(/[\w-./?%&amp;=]*)?$对于</strong>URL</strong>编码的正则匹配同样很重要。</p>
<p>3 python爬虫实战</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">from</span> typing <span class="token keyword">import</span> Pattern
<span class="token keyword">import</span> requests
<span class="token keyword">import</span> re
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">5200804</span><span class="token punctuation">,</span><span class="token number">5719275</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    url<span class="token operator">=</span><span class="token string-interpolation"><span class="token string">f'https://www.1biquge.com/8/8145/</span><span class="token interpolation"><span class="token punctuation">&#123;</span>i<span class="token punctuation">&#125;</span></span><span class="token string">.html'</span></span>
    a<span class="token operator">=</span>requests<span class="token punctuation">.</span>get<span class="token punctuation">(</span>url<span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span>
    a<span class="token punctuation">.</span>encoding<span class="token operator">=</span><span class="token string">"utf-8"</span>
    <span class="token builtin">file</span><span class="token operator">=</span><span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">"./源代码.txt"</span><span class="token punctuation">,</span><span class="token string">"w"</span><span class="token punctuation">,</span>encoding<span class="token operator">=</span><span class="token string">"utf-8"</span><span class="token punctuation">)</span>
    <span class="token builtin">file</span><span class="token punctuation">.</span>write<span class="token punctuation">(</span>a<span class="token punctuation">.</span>text<span class="token punctuation">)</span>
    <span class="token builtin">file</span><span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
    f <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">"源代码.txt"</span><span class="token punctuation">,</span> encoding<span class="token operator">=</span><span class="token string">"utf-8"</span><span class="token punctuation">)</span>
    file_handle <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">'小说文本.txt'</span><span class="token punctuation">,</span> mode<span class="token operator">=</span><span class="token string">'a'</span><span class="token punctuation">,</span>encoding<span class="token operator">=</span><span class="token string">"utf-8"</span><span class="token punctuation">)</span>
    line <span class="token operator">=</span> f<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span>
    newa <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">u'[\n\s*\r\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b\u4e00-\u9fa5]'</span><span class="token punctuation">)</span>
    b<span class="token operator">=</span>newa<span class="token punctuation">.</span>findall<span class="token punctuation">(</span>line<span class="token punctuation">)</span>
    n<span class="token operator">=</span><span class="token builtin">len</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span>n<span class="token punctuation">)</span><span class="token punctuation">:</span> 
        file_handle<span class="token punctuation">.</span>write<span class="token punctuation">(</span>b<span class="token punctuation">[</span>i<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    line <span class="token operator">=</span> f<span class="token punctuation">.</span>readline<span class="token punctuation">(</span><span class="token punctuation">)</span>
    file_handle<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
    f<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>这个实际上是一个还没有弄完的脚本，因为我爬取的是笔趣阁的小说，毕竟笔趣阁本身就是盗版网站加密也没那么麻烦，还真让我爬出来了一些信息。但是在爬取过程在还是注意到开发者是做了简易的防爬虫操作的，在一定的章节后，他随机的增加了一个四位数字，这导致了以上代码在进行无意义的空跑，但是还是发现这个代码可以应用于同网站少数小说的爬取，猜测是当时网站还没有做防爬虫的操作。</p>
<p>之后在类似的爬取其他内容过程中，发现有些网站爬虫根本无法爬取。花了一些时间研究，找到结论，我们一般写的爬虫会默认告诉服务器自己发送了一个Python爬取请求，而一般网站是不允许被爬虫访问的，估计是因为会涉及到企业根本吧。最后，我们可以通过更改User-Agent字段就可以轻易骗过该网站。</p>
<p>我们可以使用<strong>fake_useragent</strong>进行User-Agent字段的欺骗，这里就不再展示</p>
<p>最后提示一句**《刑法》第217条第1项规定，以营利为目的，未经著作权人许可，复制发行其文字作品、音乐、电影、电视、录像作品、计算机软件及其他作品，处以有期徒刑，并处或单 处罚金。 **大家未来对爬虫技术掌握爬虫技术之后，能从网络上爬取一些东西，但绝对不要违法。</p>
<h2 id="5-0-问题导入"><a href="#5-0-问题导入" class="headerlink" title="5.0 问题导入"></a>5.0 问题导入</h2><blockquote>
<p>使用 wordcloud 和 matplotlib.pyplot 方法对“222.txt”中的分词制作 分词云图，并保存为“分词云图 1.png”。 </p>
<p>使用“222.txt”中出现频率最高的前 10 个词语，制作条形图，X 轴对应 10 个词语，y 轴对应每个词语的频率值，保存成“条形图 2.png”。</p>
</blockquote>
<p>上面是上一次实验课，于刊老师的问题，下面是我解决这两道题目的方式，</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib
<span class="token keyword">import</span> pandas <span class="token keyword">as</span> pd
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token comment">#课本提示我们汉语要设置字体，不然乱码</span>
matplotlib<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'font.family'</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">'SimHei'</span>
a<span class="token operator">=</span>pd<span class="token punctuation">.</span>read_table<span class="token punctuation">(</span><span class="token string">'333.txt'</span><span class="token punctuation">,</span>encoding<span class="token operator">=</span><span class="token string">'utf-8'</span><span class="token punctuation">,</span>sep<span class="token operator">=</span><span class="token string">'       '</span><span class="token punctuation">,</span>engine<span class="token operator">=</span><span class="token string">'python'</span><span class="token punctuation">)</span>
<span class="token comment">#这个是之前李师姐讲到的读写操作</span>
df<span class="token operator">=</span>pd<span class="token punctuation">.</span>DataFrame<span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token comment">#print (a)</span>
<span class="token comment">#print(df['词语'])</span>
<span class="token comment">#测试用数据可以忽略</span>
x<span class="token operator">=</span>df<span class="token punctuation">[</span><span class="token string">'词语'</span><span class="token punctuation">]</span>
y<span class="token operator">=</span>df<span class="token punctuation">[</span><span class="token string">'次数'</span><span class="token punctuation">]</span>
figure<span class="token operator">=</span>plt<span class="token punctuation">.</span>Figure<span class="token punctuation">(</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>bar<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_1.png"></p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> pandas <span class="token keyword">as</span> pd
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token comment"># 要先pip install wordcloud</span>
<span class="token keyword">from</span> wordcloud <span class="token keyword">import</span> WordCloud
a<span class="token operator">=</span>pd<span class="token punctuation">.</span>read_table<span class="token punctuation">(</span><span class="token string">'333.txt'</span><span class="token punctuation">,</span>encoding<span class="token operator">=</span><span class="token string">'utf-8'</span><span class="token punctuation">,</span>sep<span class="token operator">=</span><span class="token string">'       '</span><span class="token punctuation">,</span>engine<span class="token operator">=</span><span class="token string">'python'</span><span class="token punctuation">)</span>
df<span class="token operator">=</span>pd<span class="token punctuation">.</span>DataFrame<span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token comment">#fit_word函数，接受字典类型，其他类型会报错</span>
dic<span class="token operator">=</span><span class="token builtin">dict</span><span class="token punctuation">(</span><span class="token builtin">zip</span><span class="token punctuation">(</span>df<span class="token punctuation">[</span><span class="token string">'词语'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>df<span class="token punctuation">[</span><span class="token string">'次数'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
wordcloud <span class="token operator">=</span> WordCloud<span class="token punctuation">(</span>font_path<span class="token operator">=</span><span class="token string">'simhei.ttf'</span><span class="token punctuation">,</span>background_color<span class="token operator">=</span><span class="token string">"white"</span><span class="token punctuation">,</span>width <span class="token operator">=</span><span class="token number">4000</span><span class="token punctuation">,</span>height<span class="token operator">=</span> <span class="token number">2000</span><span class="token punctuation">,</span>margin<span class="token operator">=</span> <span class="token number">10</span> <span class="token punctuation">)</span><span class="token punctuation">.</span>fit_words<span class="token punctuation">(</span>dic<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>wordcloud<span class="token punctuation">)</span>
<span class="token comment"># 显示</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_2.png" style="zoom:72%;" />

<p>这里使用了<strong>matplotlib库</strong>和<strong>wordcloud库</strong>两个库实现，</p>
<p>其实做 Python的数据可视化，可以使用的库分别是<strong>Matplotlib</strong>，<strong>Seaborn</strong>，<strong>Bokeh</strong>，<strong>Plotly</strong>，<strong>Pyecharts</strong>等。</p>
<p>我们课本主要将的是<strong>Matplotlib</strong>库，我其实没有在互联网上找到matplotlib库的官方中文文档的，关于这些自学起来实际上是看的浏览器的翻译，如有错误还请海涵……</p>
<h2 id="5-1-matplotlib库基础"><a href="#5-1-matplotlib库基础" class="headerlink" title="5.1 matplotlib库基础"></a>5.1 matplotlib库基础</h2><h3 id="拓展：默认值相关"><a href="#拓展：默认值相关" class="headerlink" title="拓展：默认值相关"></a>拓展：默认值相关</h3><pre class="line-numbers language-python" data-language="python"><code class="language-python">fig<span class="token operator">=</span>plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span><span class="token punctuation">)</span>
ax<span class="token operator">=</span>fig<span class="token punctuation">.</span>add_axes<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0.1</span><span class="token punctuation">,</span><span class="token number">0.1</span><span class="token punctuation">,</span><span class="token number">0.8</span><span class="token punctuation">,</span><span class="token number">0.8</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p><strong>fig.add_axes([])<strong>里面的参数分别指的距离左边，下边，坐标轴宽度，坐标轴高度，范围是(0, 1)等价于</strong>plt.axes()</strong></p>
<p><strong>ax=fig.add_axes([0.1,0.1,0.8,0.8])<strong>语句表示的是在画布中，坐标轴距离画布左边0.1倍的位置，距离下边0.1倍的位置，确定了这两个位置后，坐标轴的整体宽度和高度占0.8倍的大小，换句话说，距离右边和上边0.9<del>（0.1+0.8）</del>倍。执行如下代码，会生成一张带有坐标轴的白色，两个语句实际上都是</strong>默认值</strong>。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">pyplot<span class="token punctuation">.</span>grid<span class="token punctuation">(</span>b<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> which<span class="token operator">=</span><span class="token string">'major'</span><span class="token punctuation">,</span> axis<span class="token operator">=</span><span class="token string">'both'</span><span class="token punctuation">,</span> <span class="token operator">**</span>kwargs<span class="token punctuation">)</span>
<span class="token comment">##**kwargs 允许你将不定长度的键值对, 作为参数传递给一个函数。 如果你想要在一个函数里处理带名字的参数, 你应该使用**kwargs</span>
<span class="token comment">#以下是一个实例：</span>
plt<span class="token punctuation">.</span>grid<span class="token punctuation">(</span><span class="token boolean">True</span><span class="token punctuation">,</span> linestyle <span class="token operator">=</span> <span class="token string">"--"</span><span class="token punctuation">,</span>color <span class="token operator">=</span> <span class="token string">"gray"</span><span class="token punctuation">,</span> linewidth <span class="token operator">=</span> <span class="token string">"0.5"</span><span class="token punctuation">,</span>axis <span class="token operator">=</span> <span class="token string">'both'</span><span class="token punctuation">)</span>  
<span class="token comment"># 显示网格，这个参数默认为forse</span>
<span class="token comment"># linestyle：线型</span>
<span class="token comment"># color：颜色</span>
<span class="token comment"># linewidth：宽度</span>
<span class="token comment"># axis：x，y，both，显示x/y/两者的格网</span>
pyplot<span class="token punctuation">.</span>title<span class="token punctuation">(</span>label<span class="token punctuation">,</span> fontdict<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> loc<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> pad<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> <span class="token operator">*</span><span class="token punctuation">,</span> y<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span><span class="token operator">**</span>kwargs<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>tick_params<span class="token punctuation">(</span>bottom<span class="token operator">=</span><span class="token string">'on'</span><span class="token punctuation">,</span>top<span class="token operator">=</span><span class="token string">'off'</span><span class="token punctuation">,</span>left<span class="token operator">=</span><span class="token string">'on'</span><span class="token punctuation">,</span>right<span class="token operator">=</span><span class="token string">'off'</span><span class="token punctuation">)</span>  
<span class="token comment"># 显示刻度的那根轴线，凸出来的地方。设置为off时都不显示。默认为全显示</span>
<span class="token comment">#plt.axis('off')</span>
<span class="token comment"># 关闭坐标轴</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>如果想要了解这些具体的参数还是推荐去看下那个纯英文的matplotlib库手册</p>
<p>显示创建画布和坐标轴的好处是让我们对绘图过程有了完全的控制权（比如可以指定在什么地方绘图），而且绘图的逻辑更强 。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token comment"># 新建figure</span>
fig <span class="token operator">=</span> plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># 定义数据</span>
x <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span>
y <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span>
<span class="token comment"># 新建区域ax1</span>
<span class="token comment"># figure的百分比,从figure 10%的位置开始绘制, 宽高是figure的80%</span>
left<span class="token punctuation">,</span> bottom<span class="token punctuation">,</span> width<span class="token punctuation">,</span> height <span class="token operator">=</span> <span class="token number">0.1</span><span class="token punctuation">,</span> <span class="token number">0.1</span><span class="token punctuation">,</span> <span class="token number">0.8</span><span class="token punctuation">,</span> <span class="token number">0.8</span>
<span class="token comment"># 获得绘制的句柄</span>
ax1 <span class="token operator">=</span> fig<span class="token punctuation">.</span>add_axes<span class="token punctuation">(</span><span class="token punctuation">[</span>left<span class="token punctuation">,</span> bottom<span class="token punctuation">,</span> width<span class="token punctuation">,</span> height<span class="token punctuation">]</span><span class="token punctuation">)</span>
ax1<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> <span class="token string">'r'</span><span class="token punctuation">)</span>
ax1<span class="token punctuation">.</span>set_title<span class="token punctuation">(</span><span class="token string">'area1'</span><span class="token punctuation">)</span>
<span class="token comment"># 新增区域ax2,嵌套在ax1内</span>
left<span class="token punctuation">,</span> bottom<span class="token punctuation">,</span> width<span class="token punctuation">,</span> height <span class="token operator">=</span> <span class="token number">0.2</span><span class="token punctuation">,</span> <span class="token number">0.6</span><span class="token punctuation">,</span> <span class="token number">0.25</span><span class="token punctuation">,</span> <span class="token number">0.25</span>
<span class="token comment"># 获得绘制的句柄</span>
ax2 <span class="token operator">=</span> fig<span class="token punctuation">.</span>add_axes<span class="token punctuation">(</span><span class="token punctuation">[</span>left<span class="token punctuation">,</span> bottom<span class="token punctuation">,</span> width<span class="token punctuation">,</span> height<span class="token punctuation">]</span><span class="token punctuation">)</span>
ax2<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">)</span>
ax2<span class="token punctuation">.</span>set_title<span class="token punctuation">(</span><span class="token string">'area2'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_3.png"></p>
<blockquote>
<p> 补充知识点，在课本表5—2中出现的函数均有add_函数名(参数)和plt.函数名(参数)两种形式，实际使用中记住一种就行。</p>
</blockquote>
<h2 id="5-2-plt-plot-绘图"><a href="#5-2-plt-plot-绘图" class="headerlink" title="5.2 plt.plot()绘图"></a>5.2 plt.plot()绘图</h2><p><strong>plt.plot(x, y, c=”b”,ls=”-“, lw=2, label=“文本串”)</strong><br>其中，第二个简单的格式是最常用的格式，参数x表示指定的x轴上的数值，参数y表示指定的y轴上的数值，参数c指定绘制线条的颜色，参数ls指定的折线图的线条风格，参数lw指定绘制的线条宽度，参数label指定标记图内容的标签文本。</p>
<p>**plt.plot()<strong>函数中可以明确地给出了x,y的值，然后得到相应的(x,y)坐标点，把这些点再连接成线。当</strong>plt.plot()**函数也允许只给出y值的情况，这时系统给自动将x值指定为range(len(y))，然后根据x,y的值进行点连线。</p>
<blockquote>
<p> len() 函数返回对象中项目的数量。 </p>
</blockquote>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token comment">#只给出y值，画出相应图像</span>
plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>在5.2plt.plot()绘图这一节里，我们以实例5-1为例，</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
fig<span class="token operator">=</span>plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span><span class="token punctuation">)</span>
ax <span class="token operator">=</span> fig<span class="token punctuation">.</span>add_axes<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0.1</span><span class="token punctuation">,</span> <span class="token number">0.1</span><span class="token punctuation">,</span> <span class="token number">0.8</span><span class="token punctuation">,</span> <span class="token number">0.8</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
x<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span>
y<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">]</span>
plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">#带有默认设置的程序</span>
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
x<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span>
y<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">]</span>
plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">#去除默认值的程序</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>再pycharm环境中允许，生成的图片实际上是一模一样的，这是因为在使用了plt.plot()语句，会默认创建一个figure对象和axes坐标系。</p>
<p><strong>例5-2</strong></p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
x <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span>
y1 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span>
y2 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">]</span>
y3 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">,</span><span class="token number">15</span><span class="token punctuation">]</span>
plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y1<span class="token punctuation">,</span> color<span class="token operator">=</span><span class="token string">"g"</span><span class="token punctuation">,</span>linestyle<span class="token operator">=</span><span class="token string">"-"</span><span class="token punctuation">,</span>marker<span class="token operator">=</span><span class="token string">"o"</span><span class="token punctuation">,</span>markerfacecolor<span class="token operator">=</span><span class="token string">"g"</span><span class="token punctuation">,</span>markersize<span class="token operator">=</span><span class="token number">20</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y2<span class="token punctuation">,</span> color<span class="token operator">=</span><span class="token string">"r"</span><span class="token punctuation">,</span>linestyle<span class="token operator">=</span><span class="token string">":"</span><span class="token punctuation">,</span>marker<span class="token operator">=</span><span class="token string">"D"</span><span class="token punctuation">,</span>markerfacecolor<span class="token operator">=</span><span class="token string">"r"</span><span class="token punctuation">,</span>markersize<span class="token operator">=</span><span class="token number">20</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y3<span class="token punctuation">,</span> color<span class="token operator">=</span><span class="token string">"b"</span><span class="token punctuation">,</span>linestyle<span class="token operator">=</span><span class="token string">"-."</span><span class="token punctuation">,</span>marker<span class="token operator">=</span><span class="token string">"s"</span><span class="token punctuation">,</span>markerfacecolor<span class="token operator">=</span><span class="token string">"b"</span><span class="token punctuation">,</span>markersize<span class="token operator">=</span><span class="token number">20</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>引例：画一张二次曲线</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token comment">#从-1到1之间等间隔采66个数.也就是说所画出来的图形是66个点连接得来的</span>
<span class="token comment">#注意：如果点数过小的话会导致画出来二次函数图像不平滑</span>
x <span class="token operator">=</span> np<span class="token punctuation">.</span>linspace<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span><span class="token number">66</span><span class="token punctuation">)</span>
<span class="token comment">#linspace(start,stop,num,endpoint= True)函数在[start,stop]范围内计算返回num个均匀间隔的点，可以通过endpoint确认是否包含间隔点</span>
<span class="token comment"># 绘制x^2函数的图像</span>
y <span class="token operator">=</span> x<span class="token operator">**</span><span class="token number">2</span>
plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-python" data-language="python"><code class="language-python">numpy<span class="token punctuation">.</span>random<span class="token punctuation">.</span>randint<span class="token punctuation">(</span>low<span class="token punctuation">,</span> high<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> size<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> dtype<span class="token operator">=</span><span class="token string">'l'</span><span class="token punctuation">)</span>
<span class="token comment">#返回一个随机整型数，范围从低（包括）到高（不包括），即[low, high)。</span>
<span class="token comment">#如果没有写参数high的值，则返回[0,low)的值。</span>
<span class="token comment">#输出随机数的尺寸，比如size = (m * n* k)则输出同规模即m * n* k个随机数。默认是None的，仅仅返回满足要求的单一随机数。</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_4.png"></p>
<p>我们可以看到当图像间隔足够小的时候，折线图可以模拟出二次曲线的形式，类似的方法可以实现正弦函数的产生</p>
<p><strong>例5-4</strong></p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
x <span class="token operator">=</span> np<span class="token punctuation">.</span>linspace<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token operator">*</span>np<span class="token punctuation">.</span>pi<span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span>
y <span class="token operator">=</span> np<span class="token punctuation">.</span>sin<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">,</span>c<span class="token operator">=</span><span class="token string">'r'</span><span class="token punctuation">,</span>ls<span class="token operator">=</span><span class="token string">"-"</span><span class="token punctuation">,</span> lw<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">,</span> label<span class="token operator">=</span><span class="token string">"sin"</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>legend<span class="token punctuation">(</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_5.png"></p>
<p><strong>例5-5</strong></p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
t<span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">)</span>
x<span class="token operator">=</span> t<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">#将arange(12)生成的12个数展成[4,3]的二维数组</span>
y<span class="token operator">=</span>np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>randint<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span>size<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_6.png"></p>
<h2 id="5-3-划分子图"><a href="#5-3-划分子图" class="headerlink" title="5.3 划分子图"></a>5.3 划分子图</h2><p>例5-7:</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
x <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span>
y <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">]</span>
plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span>
x <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span>
y <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2.0</span><span class="token punctuation">,</span><span class="token number">3.5</span><span class="token punctuation">,</span><span class="token number">4.6</span><span class="token punctuation">]</span>
plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_7.png"></p>
<p>在这个情况下，两个折线是被默认存储到同一个画布上，然而我们在中间多添加一个plt.show()就会是分别位于一张画布上。在这个实例中也能看出来在，两张图片的X轴范围不同的情况下，放在同一张画布下是不容易显示的.</p>
<p>为此我们要进行划分子图</p>
<p>格式一：**plt.subplot(nrows,ncols,plot_number)**将一个画布分成“行×列”个部分，其中nrows表示要划分的行数，ncols表示要划分的列数，plot_number表示当前子图区。</p>
<p>格式二：</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">figure <span class="token operator">=</span> plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span><span class="token punctuation">)</span>
figure<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span>nrows<span class="token punctuation">,</span>ncols<span class="token punctuation">,</span>plot_number<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p><strong>实例5-8 : 一个figure对象画风两个子图实例</strong></p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
x <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span>
y <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">]</span>
figure <span class="token operator">=</span> plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span><span class="token punctuation">)</span>
axes1 <span class="token operator">=</span> figure<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span>
x <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span>
y <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2.0</span><span class="token punctuation">,</span><span class="token number">3.5</span><span class="token punctuation">,</span><span class="token number">4.6</span><span class="token punctuation">]</span>
axes2 <span class="token operator">=</span> figure<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>以格式一重写代码：</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
x <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span>
y <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">]</span>
figure <span class="token operator">=</span> plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>subplot<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span>
<span class="token comment">#补充一点课本上出现了两次的plt.show这可能是教材编写老师的失误，因为两编plt.show是得不到目标格式的</span>
x <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span>
y <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2.0</span><span class="token punctuation">,</span><span class="token number">3.5</span><span class="token punctuation">,</span><span class="token number">4.6</span><span class="token punctuation">]</span>
plt<span class="token punctuation">.</span>subplot<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_8.png"></p>
<p><strong>例5-9：设置绘图的各种参数实例</strong></p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token comment">#以下两句代码的作用是显示汉字图标题</span>
plt<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'font.sans-serif'</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'SimHei'</span><span class="token punctuation">]</span>
plt<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'axes.unicode_minus'</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token boolean">False</span>
<span class="token comment">#创建figure对象，划分子图</span>
figure <span class="token operator">=</span> plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span><span class="token punctuation">)</span>
axes1 <span class="token operator">=</span> figure<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
axes2 <span class="token operator">=</span> figure<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span>
<span class="token comment">#指定x,y列表值</span>
x <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span>
y <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">]</span>
axes1<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span>
<span class="token comment">#设置子图2的基本元素</span>
axes2<span class="token punctuation">.</span>set_title<span class="token punctuation">(</span><span class="token string">'图标题'</span><span class="token punctuation">)</span>
axes2<span class="token punctuation">.</span>set_xlabel<span class="token punctuation">(</span><span class="token string">'x轴标题'</span><span class="token punctuation">)</span>
axes2<span class="token punctuation">.</span>set_ylabel<span class="token punctuation">(</span><span class="token string">'y轴标题'</span><span class="token punctuation">)</span>
<span class="token comment">#设置坐标轴范围</span>
axes2<span class="token punctuation">.</span>set_xlim<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span>
axes2<span class="token punctuation">.</span>set_ylim<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">)</span>
<span class="token comment">#设置绘制的线型和颜色及标注内容</span>
plot1<span class="token operator">=</span>axes2<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span>marker<span class="token operator">=</span><span class="token string">'o'</span><span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token string">'r'</span><span class="token punctuation">,</span>label<span class="token operator">=</span><span class="token string">'点型红色'</span><span class="token punctuation">)</span>
plot2<span class="token operator">=</span>axes2<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span>linestyle<span class="token operator">=</span><span class="token string">'--'</span><span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token string">'b'</span><span class="token punctuation">,</span>label<span class="token operator">=</span><span class="token string">'线型蓝色'</span><span class="token punctuation">)</span>
axes2<span class="token punctuation">.</span>legend<span class="token punctuation">(</span>loc<span class="token operator">=</span><span class="token string">'upper left'</span><span class="token punctuation">)</span> <span class="token comment">#在指定位置显示标注</span>
<span class="token comment">#显示网格绘制子图2</span>
axes2<span class="token punctuation">.</span>grid<span class="token punctuation">(</span>b<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>which<span class="token operator">=</span><span class="token string">'major'</span><span class="token punctuation">,</span>axis<span class="token operator">=</span><span class="token string">'both'</span><span class="token punctuation">,</span>alpha<span class="token operator">=</span> <span class="token number">0.5</span><span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token string">'skyblue'</span><span class="token punctuation">,</span>linestyle<span class="token operator">=</span><span class="token string">'--'</span><span class="token punctuation">,</span>linewidth<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span>
axes2<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>savefig<span class="token punctuation">(</span><span class="token string">'tu1.jpg'</span><span class="token punctuation">,</span>dpi<span class="token operator">=</span><span class="token number">300</span><span class="token punctuation">)</span> <span class="token comment">#保存图形</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_9.png"></p>
<p>在python绘图中，不能直接显示汉字，但是我们经常希望绘制的图形中的标题及题注等能以汉字的形式显示，可以在代码中倒入库语句后面添加一下两句代码可以显示汉字<strong>plt.rcParams[‘font.sans-serif’]=[‘SimHei’]<strong>与</strong>plt.rcParams[‘axes.unicode_minus’]=False</strong>这两个一个是设置字符显示，一个是设置字体，<strong>SimHei</strong>这个实际上就是我们生活中常用到的<strong>黑体</strong></p>
<p><strong>例5-10:将一个figure对象划分为六个子图，分别绘制不同线型、不同颜色和标记的线条。</strong></p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
x <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span>
y <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">]</span>
figure <span class="token operator">=</span> plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span><span class="token punctuation">)</span>
axes1 <span class="token operator">=</span> figure<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
axes2 <span class="token operator">=</span> figure<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span>
axes3 <span class="token operator">=</span> figure<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span>
axes4 <span class="token operator">=</span> figure<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span>
axes5 <span class="token operator">=</span> figure<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span>
axes6 <span class="token operator">=</span> figure<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">)</span>
axes1<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span> <span class="token string">'ro'</span><span class="token punctuation">)</span> <span class="token comment"># 红色圆点</span>
axes2<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span> <span class="token string">'r-*'</span><span class="token punctuation">)</span> <span class="token comment">#红色星花直线</span>
axes3<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span> <span class="token string">'bs'</span><span class="token punctuation">)</span> <span class="token comment">#蓝色方块</span>
<span class="token comment">#axes4.plot(x,y, 'g^-.') #绿色三角点划线</span>
axes4<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span> <span class="token string">'^g-.'</span><span class="token punctuation">)</span>
axes5<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span> <span class="token string">'m8'</span><span class="token punctuation">)</span>  <span class="token comment">#洋红八边形</span>
axes6<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span> <span class="token string">'yd:'</span><span class="token punctuation">)</span> <span class="token comment">#黄色小菱形虚线</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>在绘图时需要指定线条的线型，标记的符号及线条的颜色的参数值写进同一个大括号，而且对三个参数的前后顺序没有要求。</p>
<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_10.png"></p>
<h3 id="拓展三张图如何划分？"><a href="#拓展三张图如何划分？" class="headerlink" title="拓展三张图如何划分？"></a>拓展三张图如何划分？</h3><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">def</span> <span class="token function">f</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> np<span class="token punctuation">.</span>exp<span class="token punctuation">(</span><span class="token operator">-</span>t<span class="token punctuation">)</span> <span class="token operator">*</span> np<span class="token punctuation">.</span>sin<span class="token punctuation">(</span><span class="token number">2</span> <span class="token operator">*</span> np<span class="token punctuation">.</span>pi <span class="token operator">*</span> t<span class="token punctuation">)</span>
<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    t1 <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">0.1</span><span class="token punctuation">)</span>
    t2 <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">0.2</span><span class="token punctuation">)</span>
    plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span><span class="token punctuation">)</span>
    plt<span class="token punctuation">.</span>subplot<span class="token punctuation">(</span><span class="token number">221</span><span class="token punctuation">)</span>
    plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>t1<span class="token punctuation">,</span> f<span class="token punctuation">(</span>t1<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'bo'</span><span class="token punctuation">,</span> t2<span class="token punctuation">,</span> f<span class="token punctuation">(</span>t2<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'g--'</span><span class="token punctuation">)</span>
    plt<span class="token punctuation">.</span>subplot<span class="token punctuation">(</span><span class="token number">222</span><span class="token punctuation">)</span>
    plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>t2<span class="token punctuation">,</span> np<span class="token punctuation">.</span>cos<span class="token punctuation">(</span><span class="token number">2</span> <span class="token operator">*</span> np<span class="token punctuation">.</span>pi <span class="token operator">*</span> t2<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'g--'</span><span class="token punctuation">)</span>
    plt<span class="token punctuation">.</span>subplot<span class="token punctuation">(</span><span class="token number">212</span><span class="token punctuation">)</span>
    plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_11.png"></p>
<p>这种方式是通过<strong>plt.subplot</strong>再次划分整个图片的区域，虽然我们是将图片划分为了(2,2)总共四份的区域，但是由于前两张图只上半部分，我们可以将整个图看成两部分，即(2,1)结构划分，使第三张图片占据2号位置，就是实现了三张图片的划分</p>
<p>另一种方式<strong>subplot2grid()</strong></p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">subplot2grid<span class="token punctuation">(</span>shape<span class="token punctuation">,</span> loc<span class="token punctuation">,</span> rowspan<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> colspan<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> fig<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> <span class="token operator">**</span>kwargs<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<blockquote>
<p> <strong>shape :</strong> 组合图框架形状，以元组形式传递，如2行3列可以表示为（2,3）<br><strong>loc ：</strong> 指定子图所在位置，如第一个位置是（0，0）<br><strong>rowspan</strong> ：指定子图需要跨几行<br><strong>colspan</strong> ： 指定子图需要跨几列</p>
</blockquote>
<h2 id="5-4-条形图"><a href="#5-4-条形图" class="headerlink" title="5.4:  条形图"></a>5.4:  条形图</h2><p>条形图是数据分析中使用较多的数据可视化方式之一。在条形图中可以非常直观地通过位置比较数值大小。因为在条形图中条的高度就是数值，所以一眼就可以看出数值的高底。</p>
<p><strong>bar(x, height, width=0.8, bottom=None, …, align=’center’)</strong></p>
<p>例5-11 :简单条形图</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
x <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span>
y <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">]</span>
figure <span class="token operator">=</span> plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>bar<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span> <span class="token comment">#默认颜色，默认宽度为0.8</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_12.png"></p>
<p>例5-12 : 横向条形图。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
x <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span>
y <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">]</span>
<span class="token comment"># x指定起始位置从0开始， bottom指定水平条起始位置为左侧， height指定绘制的水平条的宽度， width 指定绘制的水平条的长度。orientation指定要绘制的是水平条，color指定绘制的条形为红色。</span>
plt<span class="token punctuation">.</span>bar<span class="token punctuation">(</span>x<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> bottom<span class="token operator">=</span>y<span class="token punctuation">,</span> height<span class="token operator">=</span><span class="token number">0.5</span><span class="token punctuation">,</span> width<span class="token operator">=</span>x<span class="token punctuation">,</span> orientation<span class="token operator">=</span><span class="token string">"horizontal"</span><span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token string">'r'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_13.png">)</p>
<h3 id="5-4-2-多组条形图"><a href="#5-4-2-多组条形图" class="headerlink" title="5.4.2  多组条形图"></a>5.4.2  多组条形图</h3><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
x <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>
y1<span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">33</span><span class="token punctuation">,</span> <span class="token number">53</span><span class="token punctuation">,</span> <span class="token number">66</span><span class="token punctuation">,</span><span class="token number">77</span><span class="token punctuation">]</span>
y2 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">22</span><span class="token punctuation">,</span> <span class="token number">43</span><span class="token punctuation">,</span> <span class="token number">65</span><span class="token punctuation">,</span> <span class="token number">55</span><span class="token punctuation">,</span><span class="token number">68</span><span class="token punctuation">]</span>
y3<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">23</span><span class="token punctuation">,</span><span class="token number">35</span><span class="token punctuation">,</span><span class="token number">46</span><span class="token punctuation">,</span><span class="token number">57</span><span class="token punctuation">,</span><span class="token number">67</span><span class="token punctuation">]</span>
bar_width <span class="token operator">=</span> <span class="token number">0.3</span>
plt<span class="token punctuation">.</span>bar<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y1<span class="token punctuation">,</span> bar_width<span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token string">'y'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>bar<span class="token punctuation">(</span>x<span class="token operator">+</span>bar_width<span class="token punctuation">,</span>y2<span class="token punctuation">,</span> bar_width<span class="token punctuation">,</span> align<span class="token operator">=</span><span class="token string">"center"</span><span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token string">'r'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>bar<span class="token punctuation">(</span>x<span class="token operator">+</span>bar_width<span class="token operator">+</span>bar_width<span class="token punctuation">,</span>y3<span class="token punctuation">,</span> bar_width<span class="token punctuation">,</span> align<span class="token operator">=</span><span class="token string">"center"</span><span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token string">'b'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_14.png"></p>
<p>在绘制多组条形图进行对比的时候需要注意一点，bar_width的范围是(0,1)没错，但是多组图表需要保证几组图表的范围之和不超过1，以这个题为例，当bar_width设置为0.4三个表得到总和就超过了1，会导致重叠的发生。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
x <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>
y1<span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">33</span><span class="token punctuation">,</span> <span class="token number">53</span><span class="token punctuation">,</span> <span class="token number">66</span><span class="token punctuation">,</span><span class="token number">77</span><span class="token punctuation">]</span>
y2 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">22</span><span class="token punctuation">,</span> <span class="token number">43</span><span class="token punctuation">,</span> <span class="token number">65</span><span class="token punctuation">,</span> <span class="token number">55</span><span class="token punctuation">,</span><span class="token number">68</span><span class="token punctuation">]</span>
bar_width <span class="token operator">=</span> <span class="token number">0.5</span>
plt<span class="token punctuation">.</span>bar<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y1<span class="token punctuation">,</span> bar_width<span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token string">'b'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>bar<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y2<span class="token punctuation">,</span> bar_width<span class="token punctuation">,</span> bottom<span class="token operator">=</span>y1<span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token string">'r'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_15.png"></p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token comment">#字体设置为黑体</span>
matplotlib<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'font.family'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'SimHei'</span>
x <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>
y1<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">90</span><span class="token punctuation">,</span> <span class="token number">88</span><span class="token punctuation">,</span> <span class="token number">78</span><span class="token punctuation">,</span> <span class="token number">99</span><span class="token punctuation">,</span> <span class="token number">87</span><span class="token punctuation">]</span>
y2<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">98</span><span class="token punctuation">,</span> <span class="token number">77</span><span class="token punctuation">,</span> <span class="token number">86</span><span class="token punctuation">,</span> <span class="token number">85</span><span class="token punctuation">,</span> <span class="token number">67</span><span class="token punctuation">]</span>
bar_width <span class="token operator">=</span> <span class="token number">0.3</span>
str1 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token string">"数学"</span><span class="token punctuation">,</span><span class="token string">"英语"</span><span class="token punctuation">,</span><span class="token string">"语文"</span><span class="token punctuation">,</span><span class="token string">"计算机"</span><span class="token punctuation">,</span><span class="token string">"体育"</span><span class="token punctuation">)</span>
<span class="token comment"># 绘图</span>
plt<span class="token punctuation">.</span>bar<span class="token punctuation">(</span>x<span class="token punctuation">,</span> height<span class="token operator">=</span>y1<span class="token punctuation">,</span> width<span class="token operator">=</span>bar_width<span class="token punctuation">,</span> label<span class="token operator">=</span><span class="token string">"王晨"</span><span class="token punctuation">,</span> tick_label<span class="token operator">=</span>str1<span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token string">'b'</span><span class="token punctuation">)</span>
<span class="token comment">#tick_label下标的标签，label直接是标签</span>
plt<span class="token punctuation">.</span>bar<span class="token punctuation">(</span>x<span class="token operator">+</span>bar_width<span class="token punctuation">,</span> height<span class="token operator">=</span>y2<span class="token punctuation">,</span> width<span class="token operator">=</span>bar_width<span class="token punctuation">,</span> label<span class="token operator">=</span><span class="token string">"刘星"</span><span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token string">'r'</span><span class="token punctuation">,</span>align<span class="token operator">=</span><span class="token string">"center"</span><span class="token punctuation">)</span>
<span class="token comment">#添加图例</span>
plt<span class="token punctuation">.</span>legend<span class="token punctuation">(</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_16.png"></p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token comment"># 显示汉字设置</span>
plt<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'font.sans-serif'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'SimHei'</span><span class="token punctuation">]</span>
plt<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'axes.unicode_minus'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">False</span>
<span class="token comment"># 定义函数来显示柱状上的数值</span>
<span class="token keyword">def</span> <span class="token function">autolabel</span><span class="token punctuation">(</span>rects<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">for</span> rect <span class="token keyword">in</span> rects<span class="token punctuation">:</span>
        height <span class="token operator">=</span> rect<span class="token punctuation">.</span>get_height<span class="token punctuation">(</span><span class="token punctuation">)</span>
        plt<span class="token punctuation">.</span>text<span class="token punctuation">(</span>rect<span class="token punctuation">.</span>get_x<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> rect<span class="token punctuation">.</span>get_width<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">.</span> <span class="token operator">-</span> <span class="token number">0.2</span><span class="token punctuation">,</span> <span class="token number">1.03</span> <span class="token operator">*</span> height<span class="token punctuation">,</span> <span class="token string">'%s'</span> <span class="token operator">%</span> <span class="token builtin">float</span><span class="token punctuation">(</span>height<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    l1 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">68</span><span class="token punctuation">,</span> <span class="token number">96</span><span class="token punctuation">,</span> <span class="token number">85</span><span class="token punctuation">,</span> <span class="token number">86</span><span class="token punctuation">,</span> <span class="token number">76</span><span class="token punctuation">,</span> <span class="token number">87</span><span class="token punctuation">,</span> <span class="token number">95</span><span class="token punctuation">]</span>
    l2 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">85</span><span class="token punctuation">,</span> <span class="token number">68</span><span class="token punctuation">,</span> <span class="token number">79</span><span class="token punctuation">,</span> <span class="token number">89</span><span class="token punctuation">,</span> <span class="token number">94</span><span class="token punctuation">,</span> <span class="token number">82</span><span class="token punctuation">,</span> <span class="token number">90</span><span class="token punctuation">]</span>
    name <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'赵临'</span><span class="token punctuation">,</span> <span class="token string">'呼俞'</span><span class="token punctuation">,</span> <span class="token string">'侯亚'</span><span class="token punctuation">,</span> <span class="token string">'翟蓉'</span><span class="token punctuation">,</span> <span class="token string">'张婷'</span><span class="token punctuation">,</span> <span class="token string">'赵九'</span><span class="token punctuation">,</span> <span class="token string">'赵泽'</span><span class="token punctuation">]</span>
    total_width<span class="token punctuation">,</span> n <span class="token operator">=</span> <span class="token number">0.8</span><span class="token punctuation">,</span> <span class="token number">2</span>
    width <span class="token operator">=</span> total_width <span class="token operator">/</span> n
    x <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span>
    a <span class="token operator">=</span> plt<span class="token punctuation">.</span>bar<span class="token punctuation">(</span>x<span class="token punctuation">,</span> l1<span class="token punctuation">,</span> width<span class="token operator">=</span>width<span class="token punctuation">,</span> label<span class="token operator">=</span><span class="token string">'大学IT'</span><span class="token punctuation">,</span> fc<span class="token operator">=</span><span class="token string">'b'</span><span class="token punctuation">)</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        x<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> x<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span> width
    b <span class="token operator">=</span> plt<span class="token punctuation">.</span>bar<span class="token punctuation">(</span>x<span class="token punctuation">,</span> l2<span class="token punctuation">,</span> width<span class="token operator">=</span>width<span class="token punctuation">,</span> label<span class="token operator">=</span><span class="token string">'图像处理'</span><span class="token punctuation">,</span> tick_label<span class="token operator">=</span>name<span class="token punctuation">,</span> fc<span class="token operator">=</span><span class="token string">'r'</span><span class="token punctuation">)</span>
    autolabel<span class="token punctuation">(</span>a<span class="token punctuation">)</span>
    autolabel<span class="token punctuation">(</span>b<span class="token punctuation">)</span>
    plt<span class="token punctuation">.</span>xlabel<span class="token punctuation">(</span><span class="token string">'姓名'</span><span class="token punctuation">)</span>
    plt<span class="token punctuation">.</span>ylabel<span class="token punctuation">(</span><span class="token string">'成绩'</span><span class="token punctuation">)</span>
    plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'学生成绩'</span><span class="token punctuation">)</span>
    plt<span class="token punctuation">.</span>legend<span class="token punctuation">(</span><span class="token punctuation">)</span>
    plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_17.png"></p>
<h3 id="拓展图例的位置"><a href="#拓展图例的位置" class="headerlink" title="拓展图例的位置"></a>拓展图例的位置</h3><pre class="line-numbers language-python" data-language="python"><code class="language-python">在legend的参数中， loc参数设置图例的显示位置的：
 plt<span class="token punctuation">.</span>legend<span class="token punctuation">(</span><span class="token punctuation">[</span>l1<span class="token punctuation">,</span> l2<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'first'</span><span class="token punctuation">,</span> <span class="token string">'second'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> loc <span class="token operator">=</span> <span class="token string">'upper right'</span><span class="token punctuation">)</span><span class="token comment">#其中，loc表示位置的；</span>
<span class="token string">'best'</span>         <span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>only implemented <span class="token keyword">for</span> axes legends<span class="token punctuation">)</span><span class="token punctuation">(</span>自适应方式<span class="token punctuation">)</span>
<span class="token string">'upper right'</span>  <span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
<span class="token string">'upper left'</span>   <span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
<span class="token string">'lower left'</span>   <span class="token punctuation">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
<span class="token string">'lower right'</span>  <span class="token punctuation">:</span> <span class="token number">4</span><span class="token punctuation">,</span>
<span class="token string">'right'</span>        <span class="token punctuation">:</span> <span class="token number">5</span><span class="token punctuation">,</span>
<span class="token string">'center left'</span>  <span class="token punctuation">:</span> <span class="token number">6</span><span class="token punctuation">,</span>
<span class="token string">'center right'</span> <span class="token punctuation">:</span> <span class="token number">7</span><span class="token punctuation">,</span>
<span class="token string">'lower center'</span> <span class="token punctuation">:</span> <span class="token number">8</span><span class="token punctuation">,</span>
<span class="token string">'upper center'</span> <span class="token punctuation">:</span> <span class="token number">9</span><span class="token punctuation">,</span>
<span class="token string">'center'</span>       <span class="token punctuation">:</span> <span class="token number">10</span>
<span class="token comment">#实际上bbox_to_anchor()函数的范围是(0,1),也就是将整张图看做1 * 1大小</span>
<span class="token comment">#对于两个元素的bbox_to_anchor()，也就是(x,y)，这个参数是代表了lengend_box的起点，并且是有后面的loc决定 的。</span>
<span class="token comment">#例如，设置(0.5,0.5), loc='center'，那么代表lengend_box的中心点（center）坐标是(0.5, 0.5)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h2 id="5-5-散点图"><a href="#5-5-散点图" class="headerlink" title="5.5 散点图"></a>5.5 散点图</h2><p>在实际数据分析应用中，散点图是一种常用于观测数据的相关性的数据可视化方式。数据的相关性通常有正相关、负相关及不相关。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">plt<span class="token punctuation">.</span>scatter<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> s<span class="token punctuation">,</span> c <span class="token punctuation">,</span>marker<span class="token punctuation">,</span> alpha<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
x <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">]</span>
y <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">33</span><span class="token punctuation">,</span> <span class="token number">38</span><span class="token punctuation">,</span><span class="token number">55</span><span class="token punctuation">,</span><span class="token number">59</span><span class="token punctuation">,</span> <span class="token number">66</span><span class="token punctuation">,</span> <span class="token number">77</span><span class="token punctuation">]</span>
plt<span class="token punctuation">.</span>scatter<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">,</span>c<span class="token operator">=</span><span class="token string">'r'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="拓展：三维散点图"><a href="#拓展：三维散点图" class="headerlink" title="拓展：三维散点图"></a>拓展：三维散点图</h3><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token keyword">from</span> mpl_toolkits<span class="token punctuation">.</span>mplot3d <span class="token keyword">import</span> Axes3D  <span class="token comment"># 空间三维画图</span>
<span class="token comment"># 数据１</span>
data1 <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">24</span><span class="token punctuation">)</span><span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
x1 <span class="token operator">=</span> data1<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span>  <span class="token comment"># [ 0  3  6  9 12 15 18 21]</span>
y1 <span class="token operator">=</span> data1<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span>  <span class="token comment"># [ 1  4  7 10 13 16 19 22]</span>
z1 <span class="token operator">=</span> data1<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span>  <span class="token comment"># [ 2  5  8 11 14 17 20 23]</span>
<span class="token comment"># 数据２</span>
data2 <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>randint<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">23</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
x2 <span class="token operator">=</span> data2<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span>
y2 <span class="token operator">=</span> data2<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span>
z2 <span class="token operator">=</span> data2<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span>
<span class="token comment"># 绘制散点图</span>
fig <span class="token operator">=</span> plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span><span class="token punctuation">)</span>
ax <span class="token operator">=</span> Axes3D<span class="token punctuation">(</span>fig<span class="token punctuation">)</span>
ax<span class="token punctuation">.</span>scatter<span class="token punctuation">(</span>x1<span class="token punctuation">,</span> y1<span class="token punctuation">,</span> z1<span class="token punctuation">,</span> c<span class="token operator">=</span><span class="token string">'r'</span><span class="token punctuation">,</span> label<span class="token operator">=</span><span class="token string">'顺序点'</span><span class="token punctuation">)</span>
ax<span class="token punctuation">.</span>scatter<span class="token punctuation">(</span>x2<span class="token punctuation">,</span> y2<span class="token punctuation">,</span> z2<span class="token punctuation">,</span> c<span class="token operator">=</span><span class="token string">'g'</span><span class="token punctuation">,</span> label<span class="token operator">=</span><span class="token string">'随机点'</span><span class="token punctuation">)</span>
<span class="token comment"># 绘制图例</span>
ax<span class="token punctuation">.</span>legend<span class="token punctuation">(</span>loc<span class="token operator">=</span><span class="token string">'best'</span><span class="token punctuation">)</span>
<span class="token comment"># 添加坐标轴(顺序是Z, Y, X)</span>
ax<span class="token punctuation">.</span>set_zlabel<span class="token punctuation">(</span><span class="token string">'Z'</span><span class="token punctuation">,</span> fontdict<span class="token operator">=</span><span class="token punctuation">&#123;</span><span class="token string">'size'</span><span class="token punctuation">:</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token string">'color'</span><span class="token punctuation">:</span> <span class="token string">'red'</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
ax<span class="token punctuation">.</span>set_ylabel<span class="token punctuation">(</span><span class="token string">'Y'</span><span class="token punctuation">,</span> fontdict<span class="token operator">=</span><span class="token punctuation">&#123;</span><span class="token string">'size'</span><span class="token punctuation">:</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token string">'color'</span><span class="token punctuation">:</span> <span class="token string">'red'</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
ax<span class="token punctuation">.</span>set_xlabel<span class="token punctuation">(</span><span class="token string">'X'</span><span class="token punctuation">,</span> fontdict<span class="token operator">=</span><span class="token punctuation">&#123;</span><span class="token string">'size'</span><span class="token punctuation">:</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token string">'color'</span><span class="token punctuation">:</span> <span class="token string">'red'</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
<span class="token comment"># 展示</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_18.png"></p>
<p>拓展三维散点图，就要讲到matplotlib库里的另一个模块，mpl_toolkits.mplot3d，这个本身就来源于matplotlib不需要再行安装。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">ax<span class="token punctuation">.</span>scatter<span class="token punctuation">(</span>xs<span class="token punctuation">,</span> ys<span class="token punctuation">,</span> zs<span class="token punctuation">,</span> s<span class="token operator">=</span><span class="token number">20</span><span class="token punctuation">,</span> c<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> depthshade<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token operator">*</span>args<span class="token punctuation">,</span> <span class="token operator">*</span>kwargs<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<ul>
<li>xs,ys,zs：输入数据；</li>
<li>s:scatter点的尺寸</li>
<li>c:颜色，如c = ‘r’就是红色；</li>
<li>depthshase:透明化，True为透明，默认为True，False为不透明</li>
<li>*args等为扩展变量，如maker = ‘o’，则scatter结果为’o‘的形状</li>
</ul>
<p>我们可以看到同一个函数的参数列表基本一致。三维散点图与二维散点图的去别在于需要使用Axes3D对象或使用projection =’3d’关键字的任何其他轴一样创建。 创建一个新的matplotlib.figure.Figure并为其添加一个类型为Axes3D的新轴。</p>
<h2 id="5-6-饼图"><a href="#5-6-饼图" class="headerlink" title="5 . 6  饼图"></a>5 . 6  饼图</h2><p>由于我在制作过程中发现图片使用过多真的太大了，所以后面的代码除了我重点想为大家拓展的，其他会直接进行演示</p>
<p>饼图是一种将各项数据的大小与各项数据的总和的比例显示在一张饼中的数据可视化方式。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">plt<span class="token punctuation">.</span>pie<span class="token punctuation">(</span>x<span class="token punctuation">,</span> explode<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> labels<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> colors<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> autopct<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> pctdistance<span class="token operator">=</span><span class="token number">0.6</span><span class="token punctuation">,</span> shadow<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">,</span> labeldistance<span class="token operator">=</span><span class="token number">1.1</span><span class="token punctuation">,</span> startangle<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> radius<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> counterclock<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> wedgeprops<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> textprops<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> center<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> frame<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token comment">#添加以下两句代码，显示汉字标题</span>
plt<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'font.sans-serif'</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'SimHei'</span><span class="token punctuation">]</span>
plt<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'axes.unicode_minus'</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token boolean">False</span>
<span class="token comment"># 数据</span>
labels <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"数学"</span><span class="token punctuation">,</span> <span class="token string">"语文"</span><span class="token punctuation">,</span> <span class="token string">"英语"</span><span class="token punctuation">,</span> <span class="token string">"物理"</span><span class="token punctuation">,</span><span class="token string">"化学"</span><span class="token punctuation">]</span>
cj <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">91</span><span class="token punctuation">,</span> <span class="token number">88</span><span class="token punctuation">,</span> <span class="token number">89</span><span class="token punctuation">,</span> <span class="token number">89</span><span class="token punctuation">,</span><span class="token number">92</span><span class="token punctuation">]</span>
<span class="token comment"># 画图</span>
plt<span class="token punctuation">.</span>pie<span class="token punctuation">(</span>x<span class="token operator">=</span>cj<span class="token punctuation">,</span> labels<span class="token operator">=</span>labels<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token comment">#添加以下两句代码，显示汉字标题</span>
plt<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'font.sans-serif'</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'SimHei'</span><span class="token punctuation">]</span>
plt<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'axes.unicode_minus'</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token boolean">False</span>
<span class="token comment"># 数据</span>
labels <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"数学"</span><span class="token punctuation">,</span> <span class="token string">"语文"</span><span class="token punctuation">,</span> <span class="token string">"英语"</span><span class="token punctuation">,</span> <span class="token string">"物理"</span><span class="token punctuation">,</span><span class="token string">"化学"</span><span class="token punctuation">]</span>
cj <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">91</span><span class="token punctuation">,</span> <span class="token number">88</span><span class="token punctuation">,</span> <span class="token number">89</span><span class="token punctuation">,</span> <span class="token number">89</span><span class="token punctuation">,</span><span class="token number">92</span><span class="token punctuation">]</span>
tuchu<span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0.1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span>
<span class="token comment"># 画图</span>
plt<span class="token punctuation">.</span>pie<span class="token punctuation">(</span>x<span class="token operator">=</span>cj<span class="token punctuation">,</span> labels<span class="token operator">=</span>labels<span class="token punctuation">,</span>explode<span class="token operator">=</span>tuchu<span class="token punctuation">,</span> shadow<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>这个实例添加了两个参数，explode=tuchu, shadow=True,<code>explode=tuchu</code>控制突出显示效果，<del>其实这个突出效果是用便宜实现的，可以自己设置偏移量的多少</del>，<code>shadow=True</code>控制添加阴影效果。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token comment">#添加以下两句代码，显示汉字标题</span>
plt<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'font.sans-serif'</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'SimHei'</span><span class="token punctuation">]</span>
plt<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'axes.unicode_minus'</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token boolean">False</span>
<span class="token comment"># 数据</span>
labels <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"数学"</span><span class="token punctuation">,</span> <span class="token string">"语文"</span><span class="token punctuation">,</span> <span class="token string">"英语"</span><span class="token punctuation">,</span> <span class="token string">"物理"</span><span class="token punctuation">,</span><span class="token string">"化学"</span><span class="token punctuation">]</span>
cj <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">91</span><span class="token punctuation">,</span> <span class="token number">88</span><span class="token punctuation">,</span> <span class="token number">89</span><span class="token punctuation">,</span> <span class="token number">89</span><span class="token punctuation">,</span><span class="token number">92</span><span class="token punctuation">]</span>
tuchu <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0.1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span>
<span class="token comment"># 画图</span>
plt<span class="token punctuation">.</span>pie<span class="token punctuation">(</span>x<span class="token operator">=</span>cj<span class="token punctuation">,</span> labels<span class="token operator">=</span>labels<span class="token punctuation">,</span>explode<span class="token operator">=</span>tuchu<span class="token punctuation">,</span> shadow<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span>autopct<span class="token operator">=</span><span class="token string">"%0.1f%%"</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>legend<span class="token punctuation">(</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>本实例通过autopct=”%0.1f%%显示数据标签，其中%0.1f指定显示小数点后一位的浮点数，后面的两个%%用于显示一个%号，通过plt.legend()控制了显示图例。</p>
<h3 id="拓展：极轴饼图"><a href="#拓展：极轴饼图" class="headerlink" title="拓展：极轴饼图"></a>拓展：极轴饼图</h3><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt

np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>seed<span class="token punctuation">(</span><span class="token number">19680801</span><span class="token punctuation">)</span><span class="token comment">#生成随机数</span>

N <span class="token operator">=</span> <span class="token number">10</span><span class="token comment">#显示几个圆圈</span>
theta <span class="token operator">=</span> np<span class="token punctuation">.</span>linspace<span class="token punctuation">(</span><span class="token number">0.0</span><span class="token punctuation">,</span> <span class="token number">2</span> <span class="token operator">*</span> np<span class="token punctuation">.</span>pi<span class="token punctuation">,</span> N<span class="token punctuation">,</span> endpoint<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span>
radii <span class="token operator">=</span> <span class="token number">10</span> <span class="token operator">*</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>rand<span class="token punctuation">(</span>N<span class="token punctuation">)</span><span class="token comment">#</span>
width <span class="token operator">=</span> np<span class="token punctuation">.</span>pi <span class="token operator">/</span> <span class="token number">4</span> <span class="token operator">*</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>rand<span class="token punctuation">(</span>N<span class="token punctuation">)</span><span class="token comment">#</span>
<span class="token comment">#在添加projection='polar'之后解释器会自动绘制出一张即坐标图</span>
ax <span class="token operator">=</span> plt<span class="token punctuation">.</span>subplot<span class="token punctuation">(</span><span class="token number">111</span><span class="token punctuation">,</span> projection<span class="token operator">=</span>‘polar’<span class="token punctuation">)</span>
<span class="token comment">#bars = ax.bar(theta, radii, width=width, bottom=0.0)</span>
<span class="token comment">#前三个参数分别对应left,height,width,left表示从哪开始，表示开始 位置，</span>
<span class="token comment">#height表示从中心点向边缘绘制的长度</span>
bars <span class="token operator">=</span> ax<span class="token punctuation">.</span>bar<span class="token punctuation">(</span>theta<span class="token punctuation">,</span> radii<span class="token punctuation">,</span> width<span class="token operator">=</span>width<span class="token punctuation">,</span> bottom<span class="token operator">=</span><span class="token number">0.0</span><span class="token punctuation">)</span>
<span class="token comment">#left表示从哪开始，</span>
<span class="token comment">#radii表示从中心点向边缘绘制的长度（半径）</span>
<span class="token comment">#width表示末端的弧长</span>
<span class="token comment">#bottom意思是底部</span>
<span class="token comment">#自定义颜色和不透明度，下面代码是在极坐标系中画出扇形图的</span>
<span class="token keyword">for</span> r<span class="token punctuation">,</span> bar <span class="token keyword">in</span> <span class="token builtin">zip</span><span class="token punctuation">(</span>radii<span class="token punctuation">,</span> bars<span class="token punctuation">)</span><span class="token punctuation">:</span>
bar<span class="token punctuation">.</span>set_facecolor<span class="token punctuation">(</span>plt<span class="token punctuation">.</span>cm<span class="token punctuation">.</span>viridis<span class="token punctuation">(</span>r<span class="token operator">/</span><span class="token number">11</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">#定义颜色r/10.代表颜色与半径有关</span>
bar<span class="token punctuation">.</span>set_alpha<span class="token punctuation">(</span><span class="token number">0.3</span><span class="token punctuation">)</span><span class="token comment">#定义不透明度，越小越透明</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_19.png"></p>
<h3 id="拓展-多层嵌套饼图"><a href="#拓展-多层嵌套饼图" class="headerlink" title="拓展: 多层嵌套饼图"></a>拓展: 多层嵌套饼图</h3><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">from</span> matplotlib <span class="token keyword">import</span> pyplot <span class="token keyword">as</span> plt
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
size <span class="token operator">=</span> <span class="token number">0.25</span>
base <span class="token operator">=</span> <span class="token number">50</span>
plt<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'font.family'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'SimHei'</span>
fig<span class="token punctuation">,</span> ax <span class="token operator">=</span> plt<span class="token punctuation">.</span>subplots<span class="token punctuation">(</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
vals_middle <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span>
    <span class="token punctuation">[</span><span class="token number">47.5</span><span class="token punctuation">,</span> <span class="token number">11.7</span><span class="token punctuation">,</span> <span class="token number">15.2</span><span class="token punctuation">,</span> <span class="token number">9.6</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">44.8</span><span class="token punctuation">,</span> <span class="token number">7.5</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token number">9.2</span><span class="token punctuation">,</span> <span class="token number">68.5</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token number">1.2</span><span class="token punctuation">,</span> <span class="token number">7.2</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token number">80</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token number">1.7</span><span class="token punctuation">,</span> <span class="token number">18.9</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span>
vals_outer <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span>
    <span class="token punctuation">[</span><span class="token number">47.5</span><span class="token punctuation">,</span> <span class="token number">11.7</span><span class="token punctuation">,</span> <span class="token number">15.2</span><span class="token punctuation">,</span> <span class="token number">9.6</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">36.6</span><span class="token punctuation">,</span> <span class="token number">8.2</span><span class="token punctuation">,</span> <span class="token number">7.5</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token number">9.2</span><span class="token punctuation">,</span> <span class="token number">38.1</span><span class="token punctuation">,</span> <span class="token number">30.4</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token number">1.2</span><span class="token punctuation">,</span> <span class="token number">5.8</span><span class="token punctuation">,</span> <span class="token number">1.4</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token number">80</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token number">1.7</span><span class="token punctuation">,</span> <span class="token number">18.9</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span>
vals_inner <span class="token operator">=</span> vals_middle<span class="token punctuation">.</span><span class="token builtin">sum</span><span class="token punctuation">(</span>axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
<span class="token comment"># 最内圈使用的数值为内圈各类数据加上base</span>
vals_first <span class="token operator">=</span> vals_inner <span class="token operator">+</span> base
<span class="token triple-quoted-string string">'''
第二圈使用的数值, 因为最内圈每个类别都加上了base, 所以为了确保第二圈的数值和内圈相匹配, 第二圈的各类别要按照各自所占的比例分配各类的总数值.
'''</span>
vals_second <span class="token operator">=</span> np<span class="token punctuation">.</span>zeros<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    s_a <span class="token operator">=</span> vals_first<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
    s_b <span class="token operator">=</span> vals_middle<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">sum</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment"># 如果第二圈某类总数值为0, 则分配base.</span>
    <span class="token keyword">if</span> s_b <span class="token operator">==</span> <span class="token number">0.0</span><span class="token punctuation">:</span>
        vals_second<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> base
        <span class="token keyword">continue</span>
    <span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        vals_second<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>vals_middle<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">/</span> s_b<span class="token punctuation">)</span> <span class="token operator">*</span> s_a
<span class="token comment"># 第三圈使用的数值, 和上方同理</span>
vals_third <span class="token operator">=</span> np<span class="token punctuation">.</span>zeros<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    s_a <span class="token operator">=</span> vals_first<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
    s_b <span class="token operator">=</span> vals_outer<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">sum</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> s_b <span class="token operator">==</span> <span class="token number">0.0</span><span class="token punctuation">:</span>
        vals_third<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> base
        <span class="token keyword">continue</span>
    <span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        vals_third<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>vals_outer<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">/</span> s_b<span class="token punctuation">)</span> <span class="token operator">*</span> s_a
<span class="token comment"># 获取colormap tab20c和tab20b的颜色</span>
cmap_c <span class="token operator">=</span> plt<span class="token punctuation">.</span>get_cmap<span class="token punctuation">(</span><span class="token string">"tab20c"</span><span class="token punctuation">)</span>
cmap_b <span class="token operator">=</span> plt<span class="token punctuation">.</span>get_cmap<span class="token punctuation">(</span><span class="token string">"tab20b"</span><span class="token punctuation">)</span>
<span class="token comment"># 使用tab20c的全部颜色和tab20b中的 5至8 颜色</span>
cmap_1 <span class="token operator">=</span> cmap_c<span class="token punctuation">(</span>np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
cmap_2 <span class="token operator">=</span> cmap_b<span class="token punctuation">(</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment"># 内圈的颜色是每4个颜色中色彩最深的那个. vstack是将两类颜色叠加在一起</span>
inner_colors <span class="token operator">=</span> np<span class="token punctuation">.</span>vstack<span class="token punctuation">(</span><span class="token punctuation">(</span>cmap_1<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cmap_2<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment"># 外圈的颜色是全部24种颜色</span>
outer_colors <span class="token operator">=</span> np<span class="token punctuation">.</span>vstack<span class="token punctuation">(</span><span class="token punctuation">(</span>cmap_1<span class="token punctuation">,</span> cmap_2<span class="token punctuation">)</span><span class="token punctuation">)</span>
labels_first <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"餐厨废弃物\n&#123;&#125;万吨"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>vals_inner<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token string">"农业秸秆\n&#123;&#125;万吨"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>vals_inner<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token string">"水草\n151.2万吨"</span><span class="token punctuation">,</span>
                <span class="token string">"园林绿化\n废弃物\n&#123;&#125;万吨"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>vals_inner<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                <span class="token string">"淤泥\n432.0万吨"</span><span class="token punctuation">,</span>
                <span class="token string">"畜禽粪便\n21.6万吨"</span>
                <span class="token punctuation">]</span>
labels_seocnd <span class="token operator">=</span> <span class="token punctuation">[</span>
    <span class="token string">"未分类收集\n67.6万吨"</span><span class="token punctuation">,</span>
    <span class="token string">"生物干化\n3.7万吨"</span><span class="token punctuation">,</span>
    <span class="token string">"厌氧发酵\n10.2万吨"</span><span class="token punctuation">,</span>
    <span class="token string">"油水分离\n2.6万吨"</span><span class="token punctuation">,</span>
    <span class="token string">""</span><span class="token punctuation">,</span>
    <span class="token string">"粉碎\n46.8万吨"</span><span class="token punctuation">,</span>
    <span class="token string">"好氧发酵\n3.5万吨"</span><span class="token punctuation">,</span>
    <span class="token string">""</span><span class="token punctuation">,</span>
    <span class="token string">"未处理\n4.2万吨"</span><span class="token punctuation">,</span>
    <span class="token string">"藻水分离\n147.0万吨"</span><span class="token punctuation">,</span>
    <span class="token string">""</span><span class="token punctuation">,</span>
    <span class="token string">""</span><span class="token punctuation">,</span>
    <span class="token string">"未处理\n1.2万吨"</span><span class="token punctuation">,</span>
    <span class="token string">"粉碎\n7.2万吨"</span><span class="token punctuation">,</span>
    <span class="token string">""</span><span class="token punctuation">,</span>
    <span class="token string">""</span><span class="token punctuation">,</span>
    <span class="token string">"堆放\n432.0万吨"</span><span class="token punctuation">,</span>
    <span class="token string">""</span><span class="token punctuation">,</span>
    <span class="token string">""</span><span class="token punctuation">,</span>
    <span class="token string">""</span><span class="token punctuation">,</span>
    <span class="token string">"未处理\n0.7万吨"</span><span class="token punctuation">,</span>
    <span class="token string">"好氧发酵\n19.9万吨"</span><span class="token punctuation">,</span>
    <span class="token string">""</span><span class="token punctuation">,</span>
    <span class="token string">""</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span>
labels_third <span class="token operator">=</span> <span class="token punctuation">[</span>
    <span class="token string">"未处理\n67.5万吨"</span><span class="token punctuation">,</span>
    <span class="token string">"肥料化、发电\n3.7万吨"</span><span class="token punctuation">,</span>
    <span class="token string">"沼气、沼渣发电\n10.2万吨"</span><span class="token punctuation">,</span>
    <span class="token string">"焚烧\n2.6万吨"</span><span class="token punctuation">,</span>

    <span class="token string">""</span><span class="token punctuation">,</span>
    <span class="token string">"还田\n42.6万吨"</span><span class="token punctuation">,</span>
    <span class="token string">"燃料化\n4.2万吨"</span><span class="token punctuation">,</span>
    <span class="token string">"肥料化\n3.5万吨"</span><span class="token punctuation">,</span>

    <span class="token string">"未利用\n4.2万吨"</span><span class="token punctuation">,</span>
    <span class="token string">"燃料化\n80.2万吨"</span><span class="token punctuation">,</span>
    <span class="token string">"肥料化\n66.8万吨"</span><span class="token punctuation">,</span>
    <span class="token string">""</span><span class="token punctuation">,</span>

    <span class="token string">"未利用\n1.2万吨"</span><span class="token punctuation">,</span>
    <span class="token string">"肥料化\n5.8万吨"</span><span class="token punctuation">,</span>
    <span class="token string">"燃料化\n1.4万吨"</span><span class="token punctuation">,</span>
    <span class="token string">""</span><span class="token punctuation">,</span>

    <span class="token string">"未利用\n432.0万吨"</span><span class="token punctuation">,</span>
    <span class="token string">""</span><span class="token punctuation">,</span>
    <span class="token string">""</span><span class="token punctuation">,</span>
    <span class="token string">""</span><span class="token punctuation">,</span>

    <span class="token string">"未利用\n0.7万吨"</span><span class="token punctuation">,</span>
    <span class="token string">"肥料化\n19.9万吨"</span><span class="token punctuation">,</span>
    <span class="token string">""</span><span class="token punctuation">,</span>
    <span class="token string">""</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span>
handles<span class="token punctuation">,</span> labels <span class="token operator">=</span> ax<span class="token punctuation">.</span>pie<span class="token punctuation">(</span>vals_first<span class="token punctuation">,</span> radius<span class="token operator">=</span><span class="token number">1</span> <span class="token operator">-</span> size <span class="token operator">-</span> size<span class="token punctuation">,</span>
                         labels<span class="token operator">=</span>labels_first<span class="token punctuation">,</span>
                         labeldistance<span class="token operator">=</span><span class="token number">0.5</span><span class="token punctuation">,</span> rotatelabels<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> textprops<span class="token operator">=</span><span class="token punctuation">&#123;</span><span class="token string">'fontsize'</span><span class="token punctuation">:</span> <span class="token number">11</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
                         colors<span class="token operator">=</span>inner_colors<span class="token punctuation">,</span> wedgeprops<span class="token operator">=</span><span class="token builtin">dict</span><span class="token punctuation">(</span>width<span class="token operator">=</span>size<span class="token punctuation">,</span> edgecolor<span class="token operator">=</span><span class="token string">'w'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
ax<span class="token punctuation">.</span>pie<span class="token punctuation">(</span>vals_second<span class="token punctuation">.</span>flatten<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> radius<span class="token operator">=</span><span class="token number">1</span> <span class="token operator">-</span> size<span class="token punctuation">,</span> colors<span class="token operator">=</span>outer_colors<span class="token punctuation">,</span>
       labels<span class="token operator">=</span>labels_seocnd<span class="token punctuation">,</span>
       labeldistance<span class="token operator">=</span><span class="token number">0.7</span><span class="token punctuation">,</span> rotatelabels<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> textprops<span class="token operator">=</span><span class="token punctuation">&#123;</span><span class="token string">'fontsize'</span><span class="token punctuation">:</span> <span class="token number">11</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
       wedgeprops<span class="token operator">=</span><span class="token builtin">dict</span><span class="token punctuation">(</span>width<span class="token operator">=</span>size<span class="token punctuation">,</span> edgecolor<span class="token operator">=</span><span class="token string">'w'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
ax<span class="token punctuation">.</span>pie<span class="token punctuation">(</span>vals_third<span class="token punctuation">.</span>flatten<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> radius<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> colors<span class="token operator">=</span>outer_colors<span class="token punctuation">,</span>
       labels<span class="token operator">=</span>labels_third<span class="token punctuation">,</span>
       labeldistance<span class="token operator">=</span><span class="token number">0.8</span><span class="token punctuation">,</span> rotatelabels<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> textprops<span class="token operator">=</span><span class="token punctuation">&#123;</span><span class="token string">'fontsize'</span><span class="token punctuation">:</span> <span class="token number">11</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
       wedgeprops<span class="token operator">=</span><span class="token builtin">dict</span><span class="token punctuation">(</span>width<span class="token operator">=</span>size<span class="token punctuation">,</span> edgecolor<span class="token operator">=</span><span class="token string">'w'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'某市有机废弃物产生、处理、利用情况'</span><span class="token punctuation">,</span> fontsize<span class="token operator">=</span><span class="token number">25</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>legend<span class="token punctuation">(</span>handles<span class="token operator">=</span>handles<span class="token punctuation">,</span> labels<span class="token operator">=</span><span class="token punctuation">[</span>
    <span class="token string">"餐厨废弃物"</span><span class="token punctuation">,</span>
    <span class="token string">"农业秸秆"</span><span class="token punctuation">,</span>
    <span class="token string">"水草"</span><span class="token punctuation">,</span>
    <span class="token string">"园林绿化废弃物"</span><span class="token punctuation">,</span>
    <span class="token string">"淤泥"</span><span class="token punctuation">,</span>
    <span class="token string">"畜禽粪便"</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
           loc<span class="token operator">=</span><span class="token number">1</span>
           <span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_20.png"></p>
<p>这个例子来源于CSDN不是我自己写的，权拿过来做一个参考。</p>
<p>下面写个简单的</p>
<p>目前matplotlib的官方文档中尚无双层饼图绘制的专门介绍<del>我那个渣渣的英语即使有也多半找不到</del>，但是pie（）函数中有个饼图半径的设置<strong>radius</strong>，我们可以通过手动规定饼图半径，pie()由于是通过一层一层的堆叠，后面一层覆盖掉前面的层次，我们可以这样来实现两张图片的堆叠。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
vals1 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span>
vals2 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
fig<span class="token punctuation">,</span> ax <span class="token operator">=</span> plt<span class="token punctuation">.</span>subplots<span class="token punctuation">(</span><span class="token punctuation">)</span>
labels <span class="token operator">=</span> <span class="token string">'A'</span><span class="token punctuation">,</span> <span class="token string">'B'</span><span class="token punctuation">,</span> <span class="token string">'C'</span><span class="token punctuation">,</span> <span class="token string">'D'</span>
ax<span class="token punctuation">.</span>pie<span class="token punctuation">(</span>vals1<span class="token punctuation">,</span> radius<span class="token operator">=</span><span class="token number">1.2</span><span class="token punctuation">)</span>
ax<span class="token punctuation">.</span>pie<span class="token punctuation">(</span>vals2<span class="token punctuation">,</span> radius<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
ax<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span>aspect<span class="token operator">=</span><span class="token string">"equal"</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>进一步实现环图，这也是这个道理，使用一张纯白的饼图去覆盖上两张图片就能实现多层嵌套饼图</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
vals1 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span>
vals2 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
vals3 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>
fig<span class="token punctuation">,</span> ax <span class="token operator">=</span> plt<span class="token punctuation">.</span>subplots<span class="token punctuation">(</span><span class="token punctuation">)</span>
labels <span class="token operator">=</span> <span class="token string">'A'</span><span class="token punctuation">,</span> <span class="token string">'B'</span><span class="token punctuation">,</span> <span class="token string">'C'</span><span class="token punctuation">,</span> <span class="token string">'D'</span>
ax<span class="token punctuation">.</span>pie<span class="token punctuation">(</span>vals1<span class="token punctuation">,</span> radius<span class="token operator">=</span><span class="token number">1.2</span><span class="token punctuation">)</span>
ax<span class="token punctuation">.</span>pie<span class="token punctuation">(</span>vals2<span class="token punctuation">,</span> radius<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
ax<span class="token punctuation">.</span>pie<span class="token punctuation">(</span>vals3<span class="token punctuation">,</span>colors<span class="token operator">=</span><span class="token string">'w'</span><span class="token punctuation">,</span>radius<span class="token operator">=</span><span class="token number">0.7</span><span class="token punctuation">)</span>
ax<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span>aspect<span class="token operator">=</span><span class="token string">"equal"</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_31.png"></p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
matplotlib<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'font.family'</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">'SimHei'</span>
vals1 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span>
vals2 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
vals3<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>
fig<span class="token punctuation">,</span> ax <span class="token operator">=</span> plt<span class="token punctuation">.</span>subplots<span class="token punctuation">(</span><span class="token punctuation">)</span>
labels <span class="token operator">=</span> <span class="token string">'A'</span><span class="token punctuation">,</span> <span class="token string">'B'</span><span class="token punctuation">,</span> <span class="token string">'C'</span><span class="token punctuation">,</span> <span class="token string">'D'</span>
ax<span class="token punctuation">.</span>pie<span class="token punctuation">(</span>vals1<span class="token punctuation">,</span> radius<span class="token operator">=</span><span class="token number">1.2</span><span class="token punctuation">,</span>autopct<span class="token operator">=</span><span class="token string">'%1.1f%%'</span><span class="token punctuation">,</span>pctdistance<span class="token operator">=</span><span class="token number">0.9</span><span class="token punctuation">)</span>
ax<span class="token punctuation">.</span>pie<span class="token punctuation">(</span>vals2<span class="token punctuation">,</span> radius<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>autopct<span class="token operator">=</span><span class="token string">'%1.1f%%'</span><span class="token punctuation">,</span>pctdistance<span class="token operator">=</span><span class="token number">0.75</span><span class="token punctuation">)</span>
ax<span class="token punctuation">.</span>pie<span class="token punctuation">(</span>vals3<span class="token punctuation">,</span> radius<span class="token operator">=</span><span class="token number">0.6</span><span class="token punctuation">,</span>colors<span class="token operator">=</span><span class="token string">'w'</span><span class="token punctuation">)</span>
ax<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span>aspect<span class="token operator">=</span><span class="token string">"equal"</span><span class="token punctuation">,</span> title<span class="token operator">=</span><span class="token string">'多层嵌套饼图'</span><span class="token punctuation">)</span>
<span class="token comment">#plt.legend()</span>
plt<span class="token punctuation">.</span>legend<span class="token punctuation">(</span>labels<span class="token punctuation">,</span>bbox_to_anchor<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> loc<span class="token operator">=</span><span class="token string">'best'</span><span class="token punctuation">,</span> borderaxespad<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_32.png"></p>
<h2 id="5-7-直方图"><a href="#5-7-直方图" class="headerlink" title="5.7 直方图"></a>5.7 直方图</h2><p>在数据分析应用中，直方图是一种对数据分布情况进行表达的数据可视化形式，其横坐标通常表示统计样本，纵坐标表示某个样本对应的某个属性的度量值。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">plt<span class="token punctuation">.</span>hist<span class="token punctuation">(</span>x<span class="token punctuation">,</span> bins<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> density<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span>…<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
figure <span class="token operator">=</span> plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span><span class="token punctuation">)</span>
axes1 <span class="token operator">=</span> figure<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>
axes2 <span class="token operator">=</span> figure<span class="token punctuation">.</span>add_subplot<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span>
x <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>randint<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">,</span>size<span class="token operator">=</span><span class="token number">50</span><span class="token punctuation">)</span>
<span class="token comment">#在0到100的范围内，随机生成五十个随机数,</span>
axes1<span class="token punctuation">.</span>hist<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
axes2<span class="token punctuation">.</span>hist<span class="token punctuation">(</span>x<span class="token punctuation">,</span>bins<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token builtin">range</span><span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">,</span>histtype<span class="token operator">=</span><span class="token string">'barstacked'</span><span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token string">'r'</span><span class="token punctuation">)</span>
<span class="token comment">#这个实际上频率分布直方图，y坐标是x在一定范围内出现的次数</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_21.png"></p>
<h2 id="5-8-箱线图"><a href="#5-8-箱线图" class="headerlink" title="5.8 箱线图"></a>5.8 箱线图</h2><p>箱线图又称为盒式图、盒状图或箱形图，是一种用于显示一组数据分散情况资料的统计图。箱线图一般不受异常值的影响，是一种相对稳定的数据离散分布情况的描述方式。箱线图中显示的是一组数据的最大值、最小值、中位数、上下四分位数及异常值。</p>
<img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/箱线图.png" alt="1636276528346" style="zoom:50%;" />

<p>将整组数据等分成4份，就是四分位数，四分位数有3个<br>第1个四分位数就是通常所说的四分位数，称为下四分位数，通常用Q1表示，等于整组数据中所有数值由小到大排列后第25%的数字，第2个四分位数是中位数，用Q2表示，等于整组数据中所有数值由小到大排列后第50%的数字，第3个四分位数是上四分位数，用Q3表示，等于整组数据中所有数值由小到大排列后第75%的数字</p>
<p>箱线图的大小是由数据升序排列后，中间的50%个数据决定的。因此，前25%数据和后25%数据都无法影响箱线图，它们可以变得任意远，且不会扰动四分位值。若数据集呈标准正态分布，则中位数位于Q1和Q3中间，箱线图的中间线恰好位于上底和下底的正中央。若中位数偏向于下底，则数据集倾向于左偏态；若中位数偏向于上底，则数据集倾向于右偏态。箱线图的宽度在一定程度上反映了数据的波动程度。因为箱线图包含中间50%的数据，若它越扁，则说明数据较为集中，若它越宽，则说明数据越分散。</p>
<p>可以通过观察中位线位于箱体的哪个部分判断数据分布是否均匀，如果中位线位于箱体的中心位置附近则数据部比较均匀，反之则不均匀。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">plt<span class="token punctuation">.</span>boxplot<span class="token punctuation">(</span>x<span class="token punctuation">,</span> notch<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> sym<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> vert<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> whis<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> positions<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> widths<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> patch_artist<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> meanline<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> showmeans<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> showcaps<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> showbox<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> showfliers<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> boxprops<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> labels<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> flierprops<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> medianprops<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> meanprops<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> capprops<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> whiskerprops<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p><strong>例5-23</strong></p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
x <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span>
plt<span class="token punctuation">.</span>boxplot<span class="token punctuation">(</span>x<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p><strong>横版5-23：</strong></p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
x <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span>
plt<span class="token punctuation">.</span>boxplot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>vert <span class="token operator">=</span> <span class="token boolean">False</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_22.png"></p>
<p>例5-24：</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
x1<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span>
x2<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">]</span>
x3<span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">]</span>
plt<span class="token punctuation">.</span>boxplot<span class="token punctuation">(</span><span class="token punctuation">(</span>x1<span class="token punctuation">,</span>x2<span class="token punctuation">,</span>x3<span class="token punctuation">)</span><span class="token punctuation">,</span>labels<span class="token operator">=</span><span class="token punctuation">(</span><span class="token string">'x1'</span><span class="token punctuation">,</span><span class="token string">'x2'</span><span class="token punctuation">,</span><span class="token string">'x3'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_23.png"></p>
<p>从运行结果可以看出，x2数据有一个异常数据点，绘制在箱线图外部。</p>
<p>在进行数据预处理时，常常需要进行数据的异常值分析，来检测数据是否有错误和不合理的数据。异常值会影响整租数据，应该从本组数据中丢弃异常值。</p>
<p>例5-25：</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> pandas <span class="token keyword">as</span> pd
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
plt<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'font.family'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'SimHei'</span>
data <span class="token operator">=</span> <span class="token punctuation">&#123;</span>
<span class="token string">'苹果'</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token number">102</span><span class="token punctuation">,</span> <span class="token number">120</span><span class="token punctuation">,</span> <span class="token number">130</span><span class="token punctuation">,</span> <span class="token number">140</span><span class="token punctuation">,</span> <span class="token number">150</span><span class="token punctuation">,</span> <span class="token number">162</span><span class="token punctuation">,</span> <span class="token number">170</span><span class="token punctuation">,</span> <span class="token number">180</span><span class="token punctuation">,</span> <span class="token number">190</span><span class="token punctuation">,</span> <span class="token number">250</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token string">'西瓜'</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token number">120</span><span class="token punctuation">,</span> <span class="token number">133</span><span class="token punctuation">,</span> <span class="token number">140</span><span class="token punctuation">,</span> <span class="token number">150</span><span class="token punctuation">,</span> <span class="token number">169</span><span class="token punctuation">,</span> <span class="token number">170</span><span class="token punctuation">,</span> <span class="token number">183</span><span class="token punctuation">,</span> <span class="token number">190</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">210</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token string">'桃'</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">120</span><span class="token punctuation">,</span> <span class="token number">135</span><span class="token punctuation">,</span> <span class="token number">140</span><span class="token punctuation">,</span> <span class="token number">158</span><span class="token punctuation">,</span> <span class="token number">160</span><span class="token punctuation">,</span> <span class="token number">170</span><span class="token punctuation">,</span> <span class="token number">180</span><span class="token punctuation">,</span> <span class="token number">195</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token string">"香蕉"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token number">80</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">120</span><span class="token punctuation">,</span> <span class="token number">137</span><span class="token punctuation">,</span> <span class="token number">140</span><span class="token punctuation">,</span> <span class="token number">150</span><span class="token punctuation">,</span> <span class="token number">160</span><span class="token punctuation">,</span> <span class="token number">170</span><span class="token punctuation">,</span> <span class="token number">180</span><span class="token punctuation">,</span> <span class="token number">197</span><span class="token punctuation">]</span>
<span class="token punctuation">&#125;</span>
df <span class="token operator">=</span> pd<span class="token punctuation">.</span>DataFrame<span class="token punctuation">(</span>data<span class="token punctuation">)</span>
df<span class="token punctuation">.</span>plot<span class="token punctuation">.</span>box<span class="token punctuation">(</span>title<span class="token operator">=</span><span class="token string">"每种水果的销售额对比"</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_24.png"></p>
<h2 id="5-9小提琴图"><a href="#5-9小提琴图" class="headerlink" title="5.9小提琴图"></a>5.9小提琴图</h2><p>小提琴图是用来展示多组数据的分布状态及概率密度的一种数据可视化形式。小提琴图结合了箱线图和密度图的特征，主要用来显示数据的分布形状，特别适用于大数据统计分析。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
data<span class="token operator">=</span>np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>normal<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>violinplot<span class="token punctuation">(</span>data<span class="token punctuation">,</span>showmeans<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">,</span>showmedians<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_25.png"></p>
<h2 id="5-10热力图"><a href="#5-10热力图" class="headerlink" title="5.10热力图"></a>5.10热力图</h2><p>Seaborn是基于matplotlib库的Python可视化库， 它提供了一个高级界面来绘制有吸引力的统计图形。<br>Seaborn其实是在matplotlib的基础上进行了更高级的API封装，从而使作图更加容易，不需要经过大量的调整就能使图变得更精致。<br>绘图热力图是大数据分析应用的一种常用可视化形式，用来表示大量数据的关联关系。<br>在实际应用中，通常使用seaborn来绘制热力图。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">seaborn<span class="token punctuation">.</span>heatmap<span class="token punctuation">(</span>data<span class="token punctuation">,</span> vmin<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> vmax<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span>cmap<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> center<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> robust<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">,</span> annot<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> fmt<span class="token operator">=</span>’<span class="token punctuation">.</span>2g’<span class="token punctuation">,</span> annot_kws<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span>linewidths<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> linecolor<span class="token operator">=</span>’white’<span class="token punctuation">,</span> cbar<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> cbar_kws<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> cbar_ax<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span>square<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">,</span> xticklabels<span class="token operator">=</span>’auto’<span class="token punctuation">,</span> yticklabels<span class="token operator">=</span>’auto’<span class="token punctuation">,</span> mask<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span>…<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>热力图绘制属于Python绘图的中高级应用，在绘制前要先添加如下语句。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> seaborn <span class="token keyword">as</span> sns<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>例5-27：</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> seaborn <span class="token keyword">as</span> sns
x <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>randn<span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span>
f<span class="token punctuation">,</span> ax <span class="token operator">=</span> plt<span class="token punctuation">.</span>subplots<span class="token punctuation">(</span>nrows<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>heatmap<span class="token punctuation">(</span>x<span class="token punctuation">,</span> ax<span class="token operator">=</span>ax<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>heatmap<span class="token punctuation">(</span>x<span class="token punctuation">,</span> annot<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> ax<span class="token operator">=</span>ax<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> annot_kws<span class="token operator">=</span><span class="token punctuation">&#123;</span><span class="token string">'size'</span><span class="token punctuation">:</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token string">'weight'</span><span class="token punctuation">:</span><span class="token string">'bold'</span><span class="token punctuation">,</span> <span class="token string">'color'</span><span class="token punctuation">:</span><span class="token string">'blue'</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_26.png"></p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> seaborn <span class="token keyword">as</span> sns
x <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>randn<span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">)</span>
f<span class="token punctuation">,</span> ax <span class="token operator">=</span> plt<span class="token punctuation">.</span>subplots<span class="token punctuation">(</span>nrows<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>heatmap<span class="token punctuation">(</span>x<span class="token punctuation">,</span> cmap<span class="token operator">=</span><span class="token string">"YlGnBu_r"</span><span class="token punctuation">,</span> linewidths <span class="token operator">=</span> <span class="token number">0.05</span><span class="token punctuation">,</span> linecolor<span class="token operator">=</span> <span class="token string">'red'</span><span class="token punctuation">,</span> ax <span class="token operator">=</span> ax<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
p1 <span class="token operator">=</span> sns<span class="token punctuation">.</span>heatmap<span class="token punctuation">(</span>x<span class="token punctuation">,</span> ax<span class="token operator">=</span>ax<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cmap<span class="token operator">=</span><span class="token string">"hot"</span><span class="token punctuation">,</span> center<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> xticklabels<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span>
p2 <span class="token operator">=</span> sns<span class="token punctuation">.</span>heatmap<span class="token punctuation">(</span>x<span class="token punctuation">,</span> ax<span class="token operator">=</span>ax<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> cmap<span class="token operator">=</span><span class="token string">"OrRd"</span><span class="token punctuation">,</span> center<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span> xticklabels<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> yticklabels<span class="token operator">=</span><span class="token builtin">list</span><span class="token punctuation">(</span><span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_27.png"></p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> seaborn <span class="token keyword">as</span> sns
x <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>randn<span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span>
f<span class="token punctuation">,</span> ax <span class="token operator">=</span> plt<span class="token punctuation">.</span>subplots<span class="token punctuation">(</span>nrows<span class="token operator">=</span><span class="token number">4</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>heatmap<span class="token punctuation">(</span>x<span class="token punctuation">,</span> cmap<span class="token operator">=</span><span class="token string">"YlGnBu"</span><span class="token punctuation">,</span>annot<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> ax<span class="token operator">=</span>ax<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> annot_kws<span class="token operator">=</span><span class="token punctuation">&#123;</span><span class="token string">'size'</span><span class="token punctuation">:</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token string">'weight'</span><span class="token punctuation">:</span><span class="token string">'bold'</span><span class="token punctuation">,</span> <span class="token string">'color'</span><span class="token punctuation">:</span><span class="token string">'red'</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>heatmap<span class="token punctuation">(</span>x<span class="token punctuation">,</span> cmap<span class="token operator">=</span><span class="token string">"YlGnBu_r"</span><span class="token punctuation">,</span>annot<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> ax<span class="token operator">=</span>ax<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>heatmap<span class="token punctuation">(</span>x<span class="token punctuation">,</span> cmap<span class="token operator">=</span><span class="token string">"hot"</span><span class="token punctuation">,</span>annot<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> ax<span class="token operator">=</span>ax<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> annot_kws<span class="token operator">=</span><span class="token punctuation">&#123;</span><span class="token string">'size'</span><span class="token punctuation">:</span><span class="token number">9</span><span class="token punctuation">,</span><span class="token string">'weight'</span><span class="token punctuation">:</span><span class="token string">'bold'</span><span class="token punctuation">,</span> <span class="token string">'color'</span><span class="token punctuation">:</span><span class="token string">'blue'</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>heatmap<span class="token punctuation">(</span>x<span class="token punctuation">,</span> cmap<span class="token operator">=</span><span class="token string">"hot_r"</span><span class="token punctuation">,</span>annot<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> ax<span class="token operator">=</span>ax<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_28.png"></p>
<h2 id="5-11云图"><a href="#5-11云图" class="headerlink" title="5.11云图"></a>5.11云图</h2><p>词云图是目前文本大数据分析中常用的一种数据可视化方式，通过词云图可以直观的看到相应数据文本中的高频词。目前制作词云图使用最多的是Python库中的wordcloud库，wordcloud库中有3个主要的函数，分别是<strong>wordcloud.Woedcloud()</strong>,<strong>wordcloud.ImageColorGenerator()<strong>及</strong>wordcloud.random_color_func()</strong></p>
<p>由于于老师没有给我，这些数据，我自己从网上下载了个txt格式的红楼梦，做了一下。</p>
<p><strong>matplotlib.pyplot</strong>模块用于绘图展示，<strong>jieba</strong>库用于分词，<strong>numpy</strong>模块和<strong>PIL.image</strong>模块用于制作背景图，<strong>re</strong>用于正则匹配，</p>
<p>注意<strong>PIL.image</strong>模块的真名是pillow,不要试图使用PIL之类的找到添加这个库。</p>
<pre class="line-numbers language-none"><code class="language-none">pip install pillow<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> re
<span class="token keyword">import</span> jieba
<span class="token keyword">import</span> pandas <span class="token keyword">as</span> pd
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token comment"># 要先pip install wordcloud</span>
<span class="token keyword">from</span> wordcloud <span class="token keyword">import</span> WordCloud
<span class="token keyword">from</span> PIL <span class="token keyword">import</span> Image
f <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">"红楼梦.txt"</span><span class="token punctuation">,</span> encoding<span class="token operator">=</span><span class="token string">"utf-8"</span><span class="token punctuation">)</span>
file_handle <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">'红楼梦去字符.txt'</span><span class="token punctuation">,</span> mode<span class="token operator">=</span><span class="token string">'a'</span><span class="token punctuation">,</span>encoding<span class="token operator">=</span><span class="token string">"utf-8"</span><span class="token punctuation">)</span>
line <span class="token operator">=</span> f<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span>
a <span class="token operator">=</span> re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">u'[\u4e00-\u9fa5]+'</span><span class="token punctuation">)</span>
b<span class="token operator">=</span>a<span class="token punctuation">.</span>findall<span class="token punctuation">(</span>line<span class="token punctuation">)</span>
n<span class="token operator">=</span><span class="token builtin">len</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span>n<span class="token punctuation">)</span><span class="token punctuation">:</span>
    file_handle<span class="token punctuation">.</span>write<span class="token punctuation">(</span>b<span class="token punctuation">[</span>i<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
line <span class="token operator">=</span> f<span class="token punctuation">.</span>readline<span class="token punctuation">(</span><span class="token punctuation">)</span>
file_handle<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
f<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
f <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">"红楼梦去字符.txt"</span><span class="token punctuation">,</span> <span class="token string">"r"</span><span class="token punctuation">,</span> encoding<span class="token operator">=</span><span class="token string">'utf-8'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span>
words <span class="token operator">=</span> jieba<span class="token punctuation">.</span>lcut<span class="token punctuation">(</span>f<span class="token punctuation">)</span>  <span class="token comment"># 使用精确模式对文本进行分词</span>
counts <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span>  <span class="token comment"># 通过键值对的形式存储词语及其出现的次数</span>
<span class="token keyword">for</span> word <span class="token keyword">in</span> words<span class="token punctuation">:</span>
    <span class="token keyword">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>word<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">:</span>  <span class="token comment"># 单个汉字不计算在内</span>
        <span class="token keyword">continue</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>
        counts<span class="token punctuation">[</span>word<span class="token punctuation">]</span> <span class="token operator">=</span> counts<span class="token punctuation">.</span>get<span class="token punctuation">(</span>word<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span>  <span class="token comment"># 遍历所有词语，每出现一次其对应的值加 1</span>
lists <span class="token operator">=</span> <span class="token builtin">list</span><span class="token punctuation">(</span>counts<span class="token punctuation">.</span>items<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 将键值对转换成列表</span>
lists<span class="token punctuation">.</span>sort<span class="token punctuation">(</span>key<span class="token operator">=</span><span class="token keyword">lambda</span> x<span class="token punctuation">:</span> x<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> reverse<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>  <span class="token comment"># 根据词语出现的次数进行从大到小排序</span>
<span class="token comment">#file=open("./333.txt","w",encoding="utf")</span>
<span class="token comment">#for i in range(len(lists)):</span>
<span class="token comment">#    word,count=lists[i]</span>
<span class="token comment">#    print("&#123;0:&lt;5&#125;&#123;1:>5&#125;".format(word, count))</span>
<span class="token comment">#    file.write(word+f'&#123;count&#125;')</span>
<span class="token comment">#这一部分我是想把所有的词语都导出了写到333.txt里面，之后可能会有用</span>
<span class="token comment">#file.close()</span>
<span class="token builtin">file</span><span class="token operator">=</span><span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">"./红楼梦词频统计.txt"</span><span class="token punctuation">,</span><span class="token string">"w"</span><span class="token punctuation">,</span>encoding<span class="token operator">=</span><span class="token string">"utf"</span><span class="token punctuation">)</span>
<span class="token builtin">file</span><span class="token punctuation">.</span>write<span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f'词语       次数\n'</span></span><span class="token punctuation">)</span>
<span class="token comment">#在这里先写入表头</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    word<span class="token punctuation">,</span> count <span class="token operator">=</span> lists<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
    <span class="token builtin">file</span><span class="token punctuation">.</span>write<span class="token punctuation">(</span>word <span class="token operator">+</span> <span class="token string-interpolation"><span class="token string">f'       </span><span class="token interpolation"><span class="token punctuation">&#123;</span>count<span class="token punctuation">&#125;</span></span><span class="token string">\n'</span></span><span class="token punctuation">)</span>
<span class="token builtin">file</span><span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    word<span class="token punctuation">,</span> count <span class="token operator">=</span> lists<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
    <span class="token comment">#for循环输出频率前100的</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"&#123;0:&lt;5&#125;&#123;1:>5&#125;"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>word<span class="token punctuation">,</span> count<span class="token punctuation">)</span><span class="token punctuation">)</span>
a<span class="token operator">=</span>pd<span class="token punctuation">.</span>read_table<span class="token punctuation">(</span><span class="token string">'红楼梦词频统计.txt'</span><span class="token punctuation">,</span>encoding<span class="token operator">=</span><span class="token string">'utf-8'</span><span class="token punctuation">,</span>sep<span class="token operator">=</span><span class="token string">'       '</span><span class="token punctuation">,</span>engine<span class="token operator">=</span><span class="token string">'python'</span><span class="token punctuation">)</span>
df<span class="token operator">=</span>pd<span class="token punctuation">.</span>DataFrame<span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token comment">#fit_word函数，接受字典类型，其他类型会报错</span>
dic<span class="token operator">=</span><span class="token builtin">dict</span><span class="token punctuation">(</span><span class="token builtin">zip</span><span class="token punctuation">(</span>df<span class="token punctuation">[</span><span class="token string">'词语'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>df<span class="token punctuation">[</span><span class="token string">'次数'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#通过PIL库设置背景图片</span>
bg<span class="token operator">=</span>np<span class="token punctuation">.</span>array<span class="token punctuation">(</span>Image<span class="token punctuation">.</span><span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">"城市.png"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#当使用PlL.Image.open()打开图片后，如果要使用函数，需要先将image形式转换成array数组,这里是由于两个数组存储顺序不一致，</span>
wordcloud <span class="token operator">=</span> WordCloud<span class="token punctuation">(</span>font_path<span class="token operator">=</span><span class="token string">'simhei.ttf'</span><span class="token punctuation">,</span>mask<span class="token operator">=</span>bg<span class="token punctuation">,</span>background_color<span class="token operator">=</span><span class="token string">"white"</span><span class="token punctuation">)</span><span class="token punctuation">.</span>fit_words<span class="token punctuation">(</span>dic<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>wordcloud<span class="token punctuation">)</span>
<span class="token comment"># 显示</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="https://im-so-scared-2.gitee.io/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/Figure_29.png"></p>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/shier_jinghuang/about" rel="external nofollow noreferrer">十二惊惶</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://im-so-scared-2.gitee.io/shier_jinghuang/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/">https://im-so-scared-2.gitee.io/shier_jinghuang/shier_jinghuang/2021/11/06/python%E7%BB%98%E5%9B%BE%E5%8F%8A%E5%8F%AF%E8%A7%86%E5%8C%96%E5%A4%87%E8%AF%BE/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/shier_jinghuang/about" target="_blank">十二惊惶</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/shier_jinghuang/tags/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/">
                                    <span class="chip bg-color">基础知识</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/shier_jinghuang/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/shier_jinghuang/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/shier_jinghuang/2021/11/23/ctfshow%E8%A7%A3%E9%A2%98%E4%B9%8BMisc%E5%85%A5%E9%97%A8/">
                    <div class="card-image">
                        
                        
                        <img src="/shier_jinghuang/medias/featureimages/21.jpg" class="responsive-img" alt="ctfshow解题之Misc入门">
                        
                        <span class="card-title">ctfshow解题之Misc入门</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2021-11-23
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/shier_jinghuang/categories/CTF/" class="post-category">
                                    CTF
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/shier_jinghuang/tags/Misc/">
                        <span class="chip bg-color">Misc</span>
                    </a>
                    
                    <a href="/shier_jinghuang/tags/ctfshow/">
                        <span class="chip bg-color">ctfshow</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/shier_jinghuang/2021/10/28/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">
                    <div class="card-image">
                        
                        
                        <img src="/shier_jinghuang/medias/featureimages/3.jpg" class="responsive-img" alt="数据结构第一章 绪论">
                        
                        <span class="card-title">数据结构第一章 绪论</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2021-10-28
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/shier_jinghuang/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" class="post-category">
                                    学习笔记
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/shier_jinghuang/tags/%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/">
                        <span class="chip bg-color">学习记录</span>
                    </a>
                    
                    <a href="/shier_jinghuang/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">
                        <span class="chip bg-color">数据结构</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/shier_jinghuang/libs/codeBlock/codeBlockFuction.js"></script>


  <!-- 是否加载使用自带的 prismjs. -->
  <script type="text/javascript" src="/shier_jinghuang/libs/prism/prism.min.js"></script>


<!-- 代码语言 -->

<script type="text/javascript" src="/shier_jinghuang/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/shier_jinghuang/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/shier_jinghuang/libs/codeBlock/codeShrink.js"></script>



    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/shier_jinghuang/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/shier_jinghuang/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        display: none;
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        display: none;
        
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="503838841"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/shier_jinghuang/libs/aplayer/APlayer.min.js"></script>
<script src="/shier_jinghuang/libs/aplayer/Meting.min.js"></script>

    

    <div class="container row center-align"
         style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2019-2023</span>
            
            <a href="/shier_jinghuang/about" target="_blank">十二惊惶</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
                &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                        class="white-color">289.6k</span>
            
            
            
                
            
            
                <span id="busuanzi_container_site_pv">
                &nbsp;|&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;
                    <span id="busuanzi_value_site_pv" class="white-color"></span>
            </span>
            
            
                <span id="busuanzi_container_site_uv">
                &nbsp;|&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;
                    <span id="busuanzi_value_site_uv" class="white-color"></span>
            </span>
            
            <br>

            <!-- 运行天数提醒. -->
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/blinkfox" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:1181062873@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=1181062873" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 1181062873" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







    <a href="/shier_jinghuang/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/shier_jinghuang/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 白天和黑夜主题 -->
<div class="stars-con">
    <div id="stars"></div>
    <div id="stars2"></div>
    <div id="stars3"></div>  
</div>

<script>
    function switchNightMode() {
        $('<div class="Cuteen_DarkSky"><div class="Cuteen_DarkPlanet"></div></div>').appendTo($('body')),
        setTimeout(function () {
            $('body').hasClass('DarkMode') 
            ? ($('body').removeClass('DarkMode'), localStorage.setItem('isDark', '0'), $('#sum-moon-icon').removeClass("fa-sun").addClass('fa-moon')) 
            : ($('body').addClass('DarkMode'), localStorage.setItem('isDark', '1'), $('#sum-moon-icon').addClass("fa-sun").removeClass('fa-moon')),
            
            setTimeout(function () {
            $('.Cuteen_DarkSky').fadeOut(1e3, function () {
                $(this).remove()
            })
            }, 2e3)
        })
    }
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/shier_jinghuang/libs/materialize/materialize.min.js"></script>
    <script src="/shier_jinghuang/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/shier_jinghuang/libs/aos/aos.js"></script>
    <script src="/shier_jinghuang/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/shier_jinghuang/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/shier_jinghuang/js/matery.js"></script>

    

    

    <!-- 雪花特效 -->
    

    <!-- 鼠标星星特效 -->
    

     
        <script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
        <script src="/shier_jinghuang/libs/others/TencentCaptcha.js"></script>
        <button id="TencentCaptcha" data-appid="xxxxxxxxxx" data-cbfn="callback" type="button" hidden></button>
    

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/shier_jinghuang/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/shier_jinghuang/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/shier_jinghuang/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
